使用 MV-VM Light 根据 Silverlight 4 中的业务规则标准标记 DataGrid 行
我编写了代码,如果包含 DataGrid 单元格的行满足给定规则(假设其文本必须具有值“Incomplete”),则可以更改该单元格的前景属性。我可以通过在后面的代码中捕获 LoadingRow 事件并在那里编写我的逻辑来使这项工作变得相当容易,但我觉得这不是一个非常优雅的 MVVM 实现。代码如下:
// Sets the foreground color of th 5th cell to red if the text in the cell corresponds
// to a value specified in the ViewModel.
private void dgProfile_LoadingRow(object sender, DataGridRowEventArgs e)
{
this.dgProfile.SelectedIndex = e.Row.GetIndex();
DataGridColumn column = this.dgProfile.Columns[4];
FrameworkElement fe = column.GetCellContent(e.Row);
FrameworkElement result = GetParent(fe, typeof(DataGridCell));
if (result != null)
{
DataGridCell cell = (DataGridCell)result;
if (((TextBlock)cell.Content).Text == (this.DataContext as ProfileViewModel).strIncompleteActivityStatus) cell.Foreground = new SolidColorBrush(Colors.Red);
else cell.Foreground = new SolidColorBrush(Colors.Black);
}
}
private FrameworkElement GetParent(FrameworkElement child, Type targetType)
{
object parent = child.Parent;
if (parent != null)
{
if (parent.GetType() == targetType)
{
return (FrameworkElement)parent;
}
else
{
return GetParent((FrameworkElement)parent, targetType);
}
}
return null;
}
有人可以告诉我是否有更好的方法来使用 MVVM Light 工具包来实现这一点,也许通过 RelayCommand 和一些巧妙的数据绑定?
预先感谢您的帮助!
I've written code to change the foreground property of a DataGrid cell if the row containing that cell meets a given rule (let's say its text must have the value "Incomplete"). I can make this work fairly easy by catching the LoadingRow event in code behind and writing my logic there, but I feel like this is not a very elegant MVVM implementation. Here's the code:
// Sets the foreground color of th 5th cell to red if the text in the cell corresponds
// to a value specified in the ViewModel.
private void dgProfile_LoadingRow(object sender, DataGridRowEventArgs e)
{
this.dgProfile.SelectedIndex = e.Row.GetIndex();
DataGridColumn column = this.dgProfile.Columns[4];
FrameworkElement fe = column.GetCellContent(e.Row);
FrameworkElement result = GetParent(fe, typeof(DataGridCell));
if (result != null)
{
DataGridCell cell = (DataGridCell)result;
if (((TextBlock)cell.Content).Text == (this.DataContext as ProfileViewModel).strIncompleteActivityStatus) cell.Foreground = new SolidColorBrush(Colors.Red);
else cell.Foreground = new SolidColorBrush(Colors.Black);
}
}
private FrameworkElement GetParent(FrameworkElement child, Type targetType)
{
object parent = child.Parent;
if (parent != null)
{
if (parent.GetType() == targetType)
{
return (FrameworkElement)parent;
}
else
{
return GetParent((FrameworkElement)parent, targetType);
}
}
return null;
}
Can someone tell me if there's a better way to implement this using the MVVM Light toolkit, perhaps through RelayCommand and some clever data binding?
Thanks in advance for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以定义模板列并将前景属性绑定到返回适当 SolidColorBrush 的值转换器。
例如:
转换器:
更多详细信息请参见:
DataGrid 单元格颜色基于单元格值
You can define a template column and bind the foreground property to a value converter that returns the appropriate SolidColorBrush.
For example:
And the converter:
More details here:
DataGrid cell color based on cell value