基于领域模型的边框颜色?

发布于 2024-12-10 10:15:39 字数 521 浏览 1 评论 0原文

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

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

并且我将其显示在列表框项模板中。

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

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

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

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

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

I have a poco class in my domain model:

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

And I am displaying it in a listbox item template.

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

But what I want to do is when the HasPlayed is true, the background color of the border turns red, and when false it's green. These brushes are in a resource dictionary.

I could add a Brush to the domain model, but that defeats the seperation of concerns. I also am NOT going to use the checkbox in the future, this is just a mock up UI.

I've tried a IValueConverter, but it doesn't change when the property changes. The model does implement INotifyPropertyChanged.

How would you change the color when the property changes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夢归不見 2024-12-17 10:15:39

我猜属性更改没有被拾取,因为 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);
            }
        }
    }

I'm guessing the property change isn't being picked up because Slot doesn't implement INotifyPropertyChanged.

Try something like this:

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);
            }
        }
    }
失去的东西太少 2024-12-17 10:15:39

您传递到转换器的值似乎是一个槽位,因此绑定不会指向发生更改的属性,以便绑定更新,但是绑定需要指定一个指向相关属性的路径。属性,在本例中为 HasPlayed。 (拥有该属性的对象当然需要实现 INPC,但你说情况已经如此,对吗?)

The value you pass into your converter appears to be a Slot, so the binding does not point to the property that changes, for the binding to update however the binding needs to specify a path which points to the relevant property, in this case HasPlayed. (And the object owning the property of course needs to implement INPC, but you said that is the case already, correct?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文