简体   繁体   English

如何在Visibility属性上执行简单的XAML(WPF)条件绑定

[英]How to do a simple XAML (WPF) conditional binding on the Visibility property

I have got a view model with a property: 我有一个带有属性的视图模型:

public class MyModel
{
    public bool IsEnabled {get;set;}
}

I want to use this property to toggle a button state. 我想使用此属性来切换按钮状态。 If the boolean is true I want to hide the button, and otherwise show it. 如果布尔值为true,我想隐藏按钮,否则显示它。

I tried things like: 我尝试过这样的事情:

<Button Visibility= "{Binding IsEnabled ? Hidden : Visible  }">Enable</Button>

But this doesn't fit. 但这不合适。

I tried some more complex solution but my guess is, I am missing something trivial. 我尝试了一些更复杂的解决方案,但我的猜测是,我错过了一些微不足道的东西。

Any suggestions? 有什么建议?

Since you want to toggle between Hidden and Visible and true is hidden you can either write custom IValueConverter or use simple Style.Trigger 由于您希望在HiddenVisible之间切换并且隐藏了true,您可以编写自定义IValueConverter或使用简单的Style.Trigger

<Button Content="Enable">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Visibility" Value="Visible"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsEnabled}" Value="True">
                    <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>                    
        </Style>
    </Button.Style>
</Button>

This is all assuming the DataContext is set accordingly and MyModel.IsEnabled raises INotifyPropertyChanged.PropertyChanged event whenever changed 这是假设相应地设置DataContext并且每当更改时MyModel.IsEnabled引发INotifyPropertyChanged.PropertyChanged事件

public class MyModel : INotifyPropertyChanged
{
    private bool _isEnabled;

    public bool IsEnabled
    {
        get { return _isEnabled; }
        set
        {
            _isEnabled = value;
            OnPropertyChanged("IsEnabled");
        }
    }

    #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

Use the BooleanToVisibilityConverter: 使用BooleanToVisibilityConverter:

<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>

<Button Visibility= "{Binding IsEnabled, Converter={StaticResource BooleanToVisibilityConverter}}" />

Add a class inheriting IValueConverter 添加继承IValueConverter的类

public class BooleanToVisibilityConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool bValue = (bool)value;
        if (bValue)
            return Visibility.Visible;
        else
            return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Visibility visibility = (Visibility)value;

        if (visibility == Visibility.Visible)
            return true;
        else
            return false;
    }
    #endregion
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM