简体   繁体   English

WPF通过条件以编程方式更改行背景色

[英]WPF Programatically change row background color with conditions

I am trying to use LoadingRow for change row colors programatically. 我正在尝试使用LoadingRow以编程方式更改行颜色。 It is working as i need. 它按我的需要工作。 But the problem is with scrollbar. 但是问题出在滚动条上。 When i use the scrollbar in datagrid my formula is running again and i am getting silly ordered row colors. 当我在datagrid中使用滚动条时,我的公式再次运行,并且我得到了无序的行颜色。

here is my code. 这是我的代码。 i am trying to change colors with 17th columns value. 我试图用第17列的值更改颜色。

        private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        DataGridRow row = e.Row;
        DataRowView rView = row.Item as DataRowView;
        if (rView != null && rView.Row.ItemArray[17].ToString().Contains("1"))
        {
            renk++;  
        }
        if (renk % 2 == 0)
        {
            e.Row.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FF0000"));
        }
        else
        {
            e.Row.Background = (SolidColorBrush)(new BrushConverter().ConvertFrom("#ffffff"));
        }
    }

After running of my code it is working. 我的代码运行后,它正在工作。 But when i scrolling down my mouse to see following rows it is working also. 但是,当我向下滚动鼠标以查看以下行时,它也正在工作。 Then when i scrolling up mouse to see first rows my code does'nt work good. 然后,当我向上滚动鼠标以查看第一行时,我的代码无法正常工作。 You can see difference between opened time and after scrolling pictures below. 您可以在下面看到打开时间和滚动图片之间的时差。

一阶好 为什么令人困惑

Thanks. 谢谢。

This should work if you turn off virtualization on the DataGrid : 如果您关闭了DataGrid上的虚拟化功能,这应该可以工作:

<DataGrid
    EnableRowVirtualization="False"

It's reusing the DataGridRow objects for different rows. 它将DataGridRow对象用于不同的行。 If you've got 10,000 rows and 30 are visible, it's silly to create 10,000 DataGridRow control objects. 如果您有10,000行并且可见30行,那么创建10,000个DataGridRow控件对象是很愚蠢的。 If you've got enough items, the above will bring your application to its knees. 如果您有足够的物品,以上内容将使您的申请屈膝。 However, you may have a small number of items, in which case the above kludge is good enough. 但是,您的物品数量可能很少,在这种情况下,上述混合效果就足够了。

But you don't need to do that in any case. 但是无论如何您都不需要这样做。 The correct way to do it is to leave row virtualization enabled and work with WPF instead of against it, like the XAML below. 正确的方法是使行虚拟化保持启用状态,并使用WPF而不是与之相反,例如下面的XAML。

I don't know what your column 17 is called so I created a quickie table populated with fizz buzz items. 我不知道您的第17列叫什么,所以我创建了一个快速表,其中填充了嘶嘶声。 My equivalent of your column 17 is a column named State . 我相当于您的第17列是一个名为State的列。 If you provide a little more information we can make this match what you're actually doing. 如果您提供更多信息,我们可以使此匹配您的实际工作。

<DataGrid
    ItemsSource="{Binding FizzBuzzTable}"
    >
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding State}" Value="Fizz">
                    <Setter Property="Background" Value="LightYellow" />
                </DataTrigger>
                <DataTrigger Binding="{Binding State}" Value="Buzz">
                    <Setter Property="Background" Value="LightSkyBlue" />
                </DataTrigger>
                <DataTrigger Binding="{Binding State}" Value="FizzBuzz">
                    <Setter Property="Background" Value="GreenYellow" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

can be easier with xaml as Datagrid as property for that 使用xaml作为Datagrid作为属性可以更容易

AlternationCount = 2 AlternateCount = 2

and the brush AlternatingRowBackground 和刷子AlternatingRowBackground

You can also try 您也可以尝试

DataGrid.RowStyleSelector DataGrid.RowStyleSelector

removing virtualization is not good ! 删除虚拟化不好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM