将 SolidColorBrush 从 ViewModel 绑定到 DataGridCell/CellStyle
我有一个具有属性 int DepartmentColor 的 ViewModel。当然,我无法将 int 值绑定到 XAML 中的 CellStyle。
我应该制作一个 IntToStyleConverter 还是应该摸索 ViewModel 中的 Style 类,例如将 int 转换为 SolicColorBrush 并将其分配给 Style 等...
后者是使用 MVVM 的方法吗?
I have a ViewModel with Property int DepartmentColor. Of course I can not bind the int value to a CellStyle in XAML.
Should I make a IntToStyleConverter or should I fumble around with the Style class in the ViewModel like convert the int to a SolicColorBrush and assign it to a Style etc...
Is the latter the way to go with MVVM ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
走转换器路线。此外,我会将该属性命名为“Department”,而不是为其绑定颜色,因为这样可以使其与 UI 的耦合更加紧密。虽然会计部门可能会将其与颜色耦合,这意味着您确定它将通过
颜色
表示,而下游它可能会以其他视觉方式表示。您还可以提前创建样式,然后只需选择一种样式并通过转换器应用它,而不是尝试在后面的代码中创建它们。Go the converter route. In addition I would title the property something to the effect of
Department
and not tie color to it as it makes it more tightly coupled to the UI. While the department may be Accounting coupling that to a color implies you are certain that it will be represented via aColor
, whereas down stream it may be represented in some other visual fashion. You could also create the styles ahead of time and then simply select one and apply it via the converter, versus trying to create them in the code behind.