Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF-通过ValueConverter绑定到属性的样式未正确更新_C#_Wpf_Xaml_Binding - Fatal编程技术网

C# WPF-通过ValueConverter绑定到属性的样式未正确更新

C# WPF-通过ValueConverter绑定到属性的样式未正确更新,c#,wpf,xaml,binding,C#,Wpf,Xaml,Binding,我有一个样式转换器,定义如下: public class StyleConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value is MyStatus && targetType == typeof(S

我有一个样式转换器,定义如下:

public class StyleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is MyStatus && targetType == typeof(Style))
        {
            var status = (MyStatus)value;
            switch (status)
            {
                case MyStatus.First:
                    return Application.Current.FindResource("firstStyle");
                case MyStatus.Second:
                    return Application.Current.FindResource("secondStyle");
            }
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
    <Style x:Key="firstStyle" TargetType="Border">
        <Setter Property="Background" Value="Yellow" />
    </Style>
    <Style x:Key="secondStyle" TargetType="Border">
        <Setter Property="Background" Value="LightGreen" />
    </Style>
App.xaml
中,我定义了一些样式,如下所示:

public class StyleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is MyStatus && targetType == typeof(Style))
        {
            var status = (MyStatus)value;
            switch (status)
            {
                case MyStatus.First:
                    return Application.Current.FindResource("firstStyle");
                case MyStatus.Second:
                    return Application.Current.FindResource("secondStyle");
            }
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
    <Style x:Key="firstStyle" TargetType="Border">
        <Setter Property="Background" Value="Yellow" />
    </Style>
    <Style x:Key="secondStyle" TargetType="Border">
        <Setter Property="Background" Value="LightGreen" />
    </Style>
项目

public class Item : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private MyStatus status;
    public MyStatus Status {
        get
        {
            return status;
        }
        set
        {
            status = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Status"));
        }
    }
}
我正在将
Item
实例添加到绑定到
MyItems
observateCollection
集合中(而
MyStatus
是一个简单的枚举)
我的问题是,该样式仅在第一次正确应用,并且在我更改
项的
状态
属性

后不会发生更改。我想您正在寻找它。您可以告诉我如何在我的情况下使用它吗?也许您应该调用
FindResource(“secondStyle”)而不是
FindResource(“secordStyle”)?这只是我简化示例时的一个输入错误。CorrectedId检查状态是否确实更改(例如,通过放置断点)