基于领域模型的边框颜色?
我的域模型中有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 DataTrigger 来触发操作:
http://en.csharp-online .net/WPF_Styles_and_Control_Templates%E2%80%94Data_Triggers
You could use a DataTrigger to trigger the action:
http://en.csharp-online.net/WPF_Styles_and_Control_Templates%E2%80%94Data_Triggers
我猜属性更改没有被拾取,因为 Slot 没有实现 INotifyPropertyChanged。
尝试这样的事情:
I'm guessing the property change isn't being picked up because Slot doesn't implement INotifyPropertyChanged.
Try something like this:
您传递到转换器的值似乎是一个槽位,因此绑定不会指向发生更改的属性,以便绑定更新,但是绑定需要指定一个指向相关属性的路径。属性,在本例中为
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 caseHasPlayed
. (And the object owning the property of course needs to implement INPC, but you said that is the case already, correct?)