为什么绑定转换器会更改未绑定到GUI的属性?

时间:2014-01-14 09:21:06

标签: c# wpf binding ivalueconverter

我将文本框绑定到属性(仅文本框)并使用转换器,它只返回相反的bool值。

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,这意味着该应用程序不再可用(它自然地在后台工作)。为什么会这样?

它看起来如何:

enter image description here

1 个答案:

答案 0 :(得分:0)

实测值,

VS自动添加行到Window属性

IsEnabled="{Binding Path=CurrentProfile.isPredefined}"

但问题仍然是,如何?

相关问题