0

我有一个ListBox包含CheckBoxes 的 WPF。当 ViewModel 注意到绑定值现在已更新时,我希望 的文本颜色TextBox变为红色。我有以下 XAML,但它不工作。我可以看到IsUpdated正在查询的属性,但是当值为True颜色时,颜色没有改变。我确定我遗漏了一些明显但无法弄清楚的东西。

<ListBox MinHeight="100" ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border Padding="2" SnapsToDevicePixels="true">
                <CheckBox x:Name="_checkBox" IsChecked="{Binding Path=IsAllowed}" Content="{Binding Item}"/>
            </Border>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding IsUpdated}" Value="True">
                    <Setter TargetName="_checkBox" Property="Foreground" Value="Red"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
4

1 回答 1

2

您是否在您的 Item 类中实现 INotifyPropertyChanged(如 Matt Hamilton 所述)并在将 IsUpdated 从 false 设置为 true 时引发 PropertyChanged 事件,反之亦然。

public class Item : INotifyPropertyChanged
{
    // ...

    private bool _isUpdated;
    public bool IsUpdated
    {
        get{ return _isUpdated; }
        set {
                _isUpdated= value;
                RaisePropertyChanged("IsUpdated");
            }
    }

    // ...
    /// <summary>
    /// Occurs when a property value changes.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if(PopertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    // ...
}
于 2009-10-27T07:56:39.030 回答