2009-08-23 65 views
14
<Button IsEnabled="{Binding (Not IsDisabled)}" /> 

有没有办法用纯XAML做到这一点,否则我将不得不通过代码来做到这一点? PS。我问的问题,知道我可以创建一个布尔值转换器是这样的:XAML'NOT'运算符?

<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; 
    } 
} 
+0

我认为这是优选的<按钮的IsEnabled = “{结合的IsEnabled}”/>在肯定的。 :) – user347594 2010-06-21 03:54:52

回答

2

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

反转布尔是如此的简单: <Button IsEnabled="{qc:Binding '!$P', P={Binding IsDisabled)}" />

可加速通常需要写一些转换的时间。

+0

双向绑定是否适用于此解决方案? – 2014-11-24 10:39:51

+1

是的,根据我上面链接的站点的文档。例如:'Height =“{qc:Binding'$ P * 10',ConvertBack ='$ value * 0.1',P = {Binding TestWidth,Mode = TwoWay}}”' – Noxxys 2014-11-25 10:02:29