循环通过WPF DataGrid使用foreach [英] Looping Through WPF DataGrid Using foreach

查看:1272
本文介绍了循环通过WPF DataGrid使用foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有这一切,我试图通过WPF DataGrid 循环使用每个循环来改变错误单元格的背景颜色。我已经检查了很多问题,但我还没有找到足够的答案。到目前为止,我已经有了

  public void RunChecks()
{
const int baseColumnCount = 3;
foreach(dataGrid.Items中的DataRowView rv)
{
for(int i = baseColumnCount; i< dataGrid.Columns.Count; i ++)
{
if (!CheckForBalancedParentheses(rv.Row [i] .ToString()))
{
颜色颜色=(颜色)ColorConverter.ConvertFromString(#FF0000);
row.Background = new SolidColorBrush(color); //问题!
}
}
}
}

问题是,为了更改我的 DataGrid 中的行的背景颜色,我需要使用 DataGridRow 对象与 DataRowView rv 相关联。



如何从对象 rv获取对 DataGridRow 的引用 DataRowView )?



感谢您的时间。 p>

编辑。基于以下建议,我现在有以下样式,它可以与鼠标悬停在一起,并设置相关单元格的前后字体。但是,在上面的代码中,我在运行时如何将backcolor应用于单元格真的很失落。 XML风格是

 < Window.Resources> 
< Style TargetType ={x:Type DataGridRow}>
< Style.Triggers>
< Trigger Property =IsMouseOver
Value =True>
< Setter Property =BackgroundValue =Red/>
< Setter Property =FontWeightValue =ExtraBold/>
< / Trigger>
< /Style.Triggers>
< / Style>
< /Window.Resources>


解决方案

当您使用WPF时,避免始终直接访问UI工件。



在您中创建ModelView属性颜色,并将其绑定到DataGrid视图的单行模板的背景颜色。



因此,为了更改颜色,您将循环抛出ModelView collecton并设置/读取绑定到每一行的每个单个对象的Color属性。通过更改它,如果绑定正确设置,您将影响行UI颜色外观。



对于具体示例,您可以查看:



如何将数据网格行的背景绑定到特定颜色? / a>


All, I am attempting to loop through a WPF DataGrid using a for each loop to change the background colour of erroneous cells. I have checked many questions but I have yet to find a sufficient answer. What I have so far is

public void RunChecks()
{
    const int baseColumnCount = 3;
    foreach (DataRowView rv in dataGrid.Items)
    {
        for (int i = baseColumnCount; i < dataGrid.Columns.Count; i++)
        {
            if (!CheckForBalancedParentheses(rv.Row[i].ToString()))
            {
                Color color = (Color)ColorConverter.ConvertFromString("#FF0000");
                row.Background = new SolidColorBrush(color); // Problem!
            }
        }
    }
}

The problem is that in order to change the Background colour of a row in my DataGrid I need to work with the DataGridRow object ascociated with the DataRowView rv.

How do I get a reference to the DataGridRow from the object rv (DataRowView)?

Thanks for your time.

Edit. Based upon the advice below I now have the following style which works with the mouse over event and set the back and fore font of the relevant cell. However, I am really lost as to how to apply the backcolor to a cell at run-time in my code above. The XML style is

<Window.Resources>
    <Style TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver"
                     Value="True">
                <Setter Property="Background" Value="Red" />
                <Setter Property="FontWeight" Value="ExtraBold" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

解决方案

When you're working with WPF, avoid always accessing UI artifacts directly.

Create in you ModelView a property Color, and bind it to the background color of a single row template of your DataGrid view.

So in order to change the color you will loop throw the ModelView collecton and set/read Color property of every single object binded to every single row. By changing it, if binding is correctly settuped, you will affect row UI color appearance.

For concrete sample you can look on:

How do I bind the background of a data grid row to specific color?

这篇关于循环通过WPF DataGrid使用foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆