在Silverlight 4中使用M-V-VM Light根据业务规则标准标记DataGrid行

在Silverlight 4中使用M-V-VM Light根据业务规则标准标记DataGrid行,silverlight,mvvm,datagrid,mvvm-light,mvvm-toolkit,Silverlight,Mvvm,Datagrid,Mvvm Light,Mvvm Toolkit,我已经编写了代码来更改DataGrid单元格的前台属性,如果包含该单元格的行满足给定规则(假设其文本必须具有值“complete”)。通过在代码隐藏中捕获LoadingRow事件并在那里编写逻辑,我可以使这项工作相当容易,但我觉得这不是一个非常优雅的MVVM实现。代码如下: // Sets the foreground color of th 5th cell to red if the text in the cell corresponds // to a value specified

我已经编写了代码来更改DataGrid单元格的前台属性,如果包含该单元格的行满足给定规则(假设其文本必须具有值“complete”)。通过在代码隐藏中捕获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 toolkit实现这一点,也许是通过RelayCommand和一些巧妙的数据绑定


提前感谢您的帮助

可以定义模板列并将前台属性绑定到值转换器,该值转换器返回相应的SolidColorBrush

例如:

<data:DataGrid.Columns>
     <data:DataGridTemplateColumn>
                <data:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding MyProperty}"
                                   Foreground="{Binding MyProperty, Converter={StaticResource MyConverter}}"/>

                    </DataTemplate>
                </data:DataGridTemplateColumn.CellTemplate>
            </data:DataGridTemplateColumn>
</data:DataGrid.Columns>
详情如下:


我将向ViewModel添加一个布尔属性,实现BooleanToVisibilityConverter,向单元格模板添加一个矩形,并使用转换器将其绑定到属性。
public class MyConverter : System.Windows.Data.IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null)
        {
            string propertyValue = (string)value;

            if (propertyValue == strIncompleteActivityStatus)
                return new SolidColorBrush(Colors.Red);
            else
                return new SolidColorBrush(Colors.Black);
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}