0

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

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();
    }
}

App.xaml我有一些样式定义如下:

    <Style x:Key="firstStyle" TargetType="Border">
        <Setter Property="Background" Value="Yellow" />
    </Style>
    <Style x:Key="secondStyle" TargetType="Border">
        <Setter Property="Background" Value="LightGreen" />
    </Style>

并在Window.xaml

    <MyApp:StyleConverter x:Key="StyleConverter" />
    <DataTemplate DataType="{x:Type MyApp:Item}">
        <Border x:Name="ItemBorder"
                Style="{Binding Path=Status, Mode=OneWay, Converter={StaticResource StyleConverter}}">
             <!-- some content here -->
        </Border>
    </DataTemplate>
    <ItemsControl x:Name="MyItems" />

Item

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实例(并且是一个简单的枚举)我的问题是样式仅在第一次正确应用并且在我更改属性后没有改变ObservableCollection<Item>MyItemsMyStatusStatusItem

4

0 回答 0