使用 MV-VM Light 根据 Silverlight 4 中的业务规则标准标记 DataGrid 行

发布于 2024-11-04 11:08:00 字数 1526 浏览 0 评论 0原文

我编写了代码,如果包含 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 技术交流群。

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

发布评论

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

评论(1

临走之时 2024-11-11 11:08:00

您可以定义模板列并将前景属性绑定到返回适当 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>

转换器:

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();
    }
}

更多详细信息请参见:

DataGrid 单元格颜色基于单元格值

You can define a template column and bind the foreground property to a value converter that returns the appropriate SolidColorBrush.

For example:

<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>

And the converter:

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();
    }
}

More details here:

DataGrid cell color based on cell value

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