17
<Button IsEnabled="{Binding (Not IsDisabled)}" />

有没有办法用纯 xaml 来做,或者我必须通过代码来做?PS。我问了这个问题,知道我可以像这样创建一个布尔转换器:

<ValueConversion(GetType(Boolean), GetType(Boolean))> 
Public Class BooleanFlagSwitchConverter : Implements IValueConverter

    Public Function Convert(value As Object, targetType As Type, 
            parameter As Object, culture As CultureInfo) As Object 
            Implements IValueConverter.Convert
        Return Not value
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, 
            parameter As Object, ByVal culture As CultureInfo) As Object 
            Implements IValueConverter.ConvertBack
        Return Not value
    End Function

End Class

[ValueConversion(typeof(bool), typeof(bool))]
public class BooleanFlagSwitchConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, 
        CultureInfo culture)
    {
        return !(bool)value;
    }

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

2 回答 2

13

它不存在。查看Binding类的属性。除了可以做你想做的事情的转换器之外,别无他法。

于 2009-08-23T02:37:15.307 回答
2

我建议使用https://quickconverter.codeplex.com/

反转一个布尔值就像这样简单: <Button IsEnabled="{qc:Binding '!$P', P={Binding IsDisabled)}" />

这加快了编写转换器通常所需的时间。

于 2014-08-07T13:37:08.947 回答