【发布时间】:2014-02-02 06:59:58
【问题描述】:
我将我的文本框绑定到属性(仅文本框)并使用了只返回相反布尔值的转换器。
XAML
<TextBox Height="23" HorizontalAlignment="Left" Margin="13,41,0,0" Text="{Binding Path=CurrentProfile.profileName}" IsEnabled="{Binding Path=CurrentProfile.isPredefined, Converter={StaticResource PredefinedToControlEnabled}}" VerticalAlignment="Top" Width="247" Grid.ColumnSpan="2" TabIndex="1" />
转换器
public class PredefinedToControlEnabled: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var isPredefined = (bool)value;
return !isPredefined;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
当属性值 isPredefined 为 true 时,TextBox 的 IsEnabled 属性正确设置为 false。当 isPredefined 为 false 时,转换器返回 false 并且 GUI 上的所有控件将 IsEnabled 属性设置为 false,这意味着应用程序不再可用(它自然地在后台工作)。为什么会这样?
它是什么样子的:
【问题讨论】:
标签: c# wpf binding ivalueconverter