我所面臨的結合一個布爾值visibility屬性的問題,所以我已經實現了我自己的布爾能見度轉換器,我使用它與我的大多數應用程序。
下面的類添加到您的應用程序:
public class BoolVisibilityConverter : IValueConverter{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture){
bool isVisible = (bool)value;
return isVisible ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){
System.Windows.Visibility currVisibility = (System.Windows.Visibility)value;
return (currVisibility == System.Windows.Visibility.Visible);
}
}
現在使用它,你就需要將其添加爲您的XAML代碼的資源。
<UserControl.Resources>
<Helpers:BoolVisibilityConverter x:Key="boolVisibilityConverter" />
</UserControl.Resources>
在您的示例使用以下命令:
<TextBlock Text="Price" Visibility="{Binding PriceVisibility, Mode=OneWay, Converter={StaticResource boolVisibilityConverter}}"/>
<TextBox Text="{Binding TicketPrice, Mode=TwoWay}" Visibility="{Binding PriceVisibility, Mode=OneWay, Converter={StaticResource boolVisibilityConverter}}"/>
感謝您的例子。你知道爲什麼ReSharper告訴:「nullable.HasValue」總是對的嗎? – Budda 2010-09-07 23:51:09