1

我的域模型中有一个 poco 类:

public class Slot
{
    bool HasPlayed { get; set; }
}

我在列表框项目模板中显示它。

<Border Background="...">
    <CheckBox IsChecked="{Binding Path=HasPlayed, Mode=TwoWay}" />
</Border>

但是我想做的是当 HasPlayed 为真时,边框的背景颜色变为红色,当为假时为绿色。这些画笔在资源字典中。

我可以将 Brush 添加到域模型中,但这会破坏关注点的分离。我也不会在未来使用复选框,这只是一个模拟 UI。

我已经尝试过 IValueConverter,但是当属性更改时它不会改变。该模型确实实现了 INotifyPropertyChanged。

当属性改变时你会如何改变颜色?

4

3 回答 3

1

您可以使用 DataTrigger 来触发操作:

http://en.csharp-online.net/WPF_Styles_and_Control_Templates%E2%80%94Data_Triggers

于 2011-10-15T23:20:18.330 回答
0

我猜属性更改没有被拾起,因为 Slot 没有实现 INotifyPropertyChanged。

尝试这样的事情:

public class Slot : INotifyPropertyChanged
    {
        private bool _hasPlayed;

        private bool HasPlayed
        {
            get { return _hasPlayed; }
            set
            {
                _hasPlayed = value;
                InvokePropertyChanged(new PropertyChangedEventArgs("HasPlayed"));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public void InvokePropertyChanged(PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }
    }
于 2011-10-15T23:21:23.477 回答
0

您传递给转换器的值似乎是 a Slot,因此绑定不指向更改的属性,以便绑定更新但是绑定需要指定指向相关属性的路径,在这种情况下是HasPlayed。(并且拥有该属性的对象当然需要实现INPC,但是您说已经是这种情况了,对吗?)

于 2011-10-15T23:30:25.050 回答