2009-08-23 104 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