WPF - 按钮的绑定可见性

时间:2015-03-30 14:59:39

标签: c# wpf

我想将单个属性绑定到Visiblity的两个按钮。我正在使用BooleantoVisibility Converter。我可以根据Property值隐藏或显示按钮。我的问题是,我只想显示两个按钮中的一个。以下是我的代码。有没有办法使用“NOT”进行绑定,或者我必须创建一个新属性?

 <telerik:RadButton Content="Close" x:Name="btnClose" Visibility="{Binding Path=IsNewRecord, Converter={StaticResource BoolToVisiblity}}" Command="{Binding CloseCommand}" CommandParameter="{Binding ElementName=ProductCombobox, Path=Text}"/>
 <telerik:RadButton Content="Delete" x:Name="btnDelete" Visibility="{Binding Path=IsNewRecord, Converter={StaticResource BoolToVisiblity}}" Command="{Binding DeleteCommand}" CommandParameter="{Binding ElementName=ProductCombobox, Path=Text}"/>

1 个答案:

答案 0 :(得分:2)

创建InvertBooleanToVisibility转换器。将相同的属性绑定到两个转换器。

public class InvertBooleanToVisibilityConverter : IValueConverter
{       
    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
        var boolValue = (bool)value;            

        return boolValue ? Visibility.Collapsed : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        return null;
    }
}

在XAML中

<UserControl.Resources>
  <Converters:InvertBooleanToVisibilityConverter x:Key="InvertConverter"/>
</UserControl.Resources>

<telerik:RadButton Content="Delete" x:Name="btnDelete" 
                   Visibility="{Binding Path=IsNewRecord, Converter={StaticResource InvertConverter}}"
                   Command="{Binding DeleteCommand}" CommandParameter="{Binding ElementName=ProductCombobox, Path=Text}"/>
相关问题