web-dev-qa-db-ja.com

ボタンの可視性をViewModelのブール値にバインドする

ボタンの可視性をViewModelのブール値にバインドするにはどうすればよいですか?

<Button Height="50" Width="50" Style="{StaticResource MyButtonStyle}"
    Command="{Binding SmallDisp}" CommandParameter="{Binding}" Cursor="Hand"
    Visibility="{Binding Path=AdvancedFormat}" />
113
raym0nd

ビューモデルへのコンバーターまたは変更を必要としない3番目の方法があります。スタイルを使用します。

<Style TargetType="Button">
   <Setter Property="Visibility" Value="Collapsed"/>
   <Style.Triggers>
      <DataTrigger Binding="{Binding IsVisible}" Value="True">
         <Setter Property="Visibility" Value="Visible"/>
      </DataTrigger>
   </Style.Triggers>
</Style>

私がバインドしているものがnot boolean--例えばDataContextがnullでない場合にのみ要素を表示するか、ビューモデルの列挙の設定に基づいて異なるレイアウトが表示されるマルチステート表示を実装します。

90
Robert Rossney

C#でのブールから可視性への2方向変換

using System;
using System.Windows;
using System.Windows.Data;

namespace FaceTheWall.converters
{
    class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Boolean && (bool)value)
            {
                return Visibility.Visible;
            }
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Visibility && (Visibility)value == Visibility.Visible)
            {
                return true;
            }
            return false;
        }
    }
}
10
Berty

一般に、それを行うには2つの方法があります。コンバータクラス、または本質的に値を変換するViewmodelのプロパティです。

それが1回限りの変換である場合、プロパティアプローチを使用する傾向があります。再利用する場合は、コンバーターを使用します。以下に、コンバーターの例を示します。

<ValueConversion(GetType(Boolean), GetType(Visibility))> _
Public Class BoolToVisibilityConverter
    Implements IValueConverter

    Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert

        If value IsNot Nothing Then
            If value = True Then 
                Return Visibility.Visible
            Else
                Return Visibility.Collapsed
            End If
        Else
            Return Visibility.Collapsed
        End If
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Throw New NotImplementedException
    End Function
End Class

ViewModelプロパティメソッドは、ブールプロパティ値をチェックし、それに基づいて可視性を返します。 INotifyPropertyChangedを実装し、BooleanプロパティとVisibilityプロパティの両方で呼び出して、正しく更新するようにしてください。

4
CodeWarrior

ビューで:

<Button
 Height="50" Width="50"
 Style="{StaticResource MyButtonStyle}"
 Command="{Binding SmallDisp}" CommandParameter="{Binding}" 
Cursor="Hand" Visibility="{Binding Path=AdvancedFormat}"/>

ビューモデル:

public _advancedFormat = Visibility.visible (whatever you start with)

public Visibility AdvancedFormat
{
 get{return _advancedFormat;}
 set{
   _advancedFormat = value;
   //raise property changed here
}

プロパティ変更イベントが必要になります

 protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) 
    { 
        PropertyChanged.Raise(this, e); 
    } 

    protected void OnPropertyChanged(string propertyName) 
    { 
        OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); 
    } 

これは彼らがModel-view-viewmodelを使用する方法です

ただし、ブール値にバインドするため、コンバータが必要になります。もう1つの方法は、ブール値を外部に設定し、そのボタンをクリックしたときにproperty_advancedFormatを目的の可視性に設定することです。

3
Kevin

これは非常に簡単な方法で実現できます。1.これをビューに記述します。

<Button HorizontalAlignment="Center" VerticalAlignment="Center" Width="50" Height="30">
<Button.Style>
        <Style TargetType="Button">
                <Setter Property="Visibility" Value="Collapsed"/>
                        <Style.Triggers>
                                <DataTrigger Binding="{Binding IsHide}" Value="True">
                                        <Setter Property="Visibility" Value="Visible"/>
                                    </DataTrigger>
                            </Style.Triggers>
            </Style>
    </Button.Style>
  1. 以下は、true/false値を保持するBooleanプロパティです。以下はコードスニペットです。私の例では、このプロパティはUserNoteクラスにあります。

    public bool _isHide = false;
    
    public bool IsHide
    {
    
    get { return _isHide; }
    
    set
        {
            _isHide = value;
                OnPropertyChanged("IsHide");
        }
    } 
    
  2. これは、IsHideプロパティが値を取得する方法です。

    userNote.IsHide = userNote.IsNoteDeleted;
    
2
Joy Fernandes