RowVirtualization导致行的背景颜色不正确

RowVirtualization导致行的背景颜色不正确

本文介绍了RowVirtualization导致行的背景颜色不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,有一些Datagrid在一些页面。这个datagrid需要一次加载5000行(分页不是我的选择),这需要很多时间。我设置EnableRowVirtualization = True,现在性能可以接受,但这里有一个问题。
在我的datagrid中,我需要根据列值(例如STATUS)将不同的背景颜色设置为不同的行,将EnableRowVirtualization从False更改为True,当我滚动时导致错误的着色。



----编辑----



这是我的XAML代码:

 < my:DataGrid Name =dgDataGridDockPanel.Dock =TopAutoGenerateColumns =FalseClipboardCopyMode =ExcludeHeader
CanUserDeleteRows =TrueRowHeight =20SelectionMode =ExtendedSelectionUnit =FullRowFontFamily =Tahoma
ItemsSource ={Binding}VirtualizingStackPanel.VirtualizationMode =RecyclingVirtualizingStackPanel.IsVirtualizing =True
EnableRowVirtualization =TrueEnableColumnVirtualization =FalseIsSynchronizedWithCurrentItem =TrueBorderBrush =Blue
RowBackground =WhiteHorizo​​ntalGridLinesBrush =BlueGridLinesVisibility =Horizo​​ntalVerticalGridLinesBrush =Blue
IsTextSearchEnabled =FalseIsTabStop =TrueHeadersVisibility =AllLoaded =dgDataGrid_Loaded
ContextMenuOpening =dgDataGrid_ContextMenuOpenin gLoadingRow =dgDataGrid_LoadingRow
ScrollViewer.IsDeferredScrollingEnabled =True>
< my:DataGrid.Resources>

< / my:DataGrid.Resources>

< my:DataGrid.RowHeaderTemplate>
< DataTemplate>
< TextBlock Text ={Binding RelativeSource = {RelativeSource Mode = FindAncestor,AncestorType = {x:Type my:DataGridRow}},Path = Header}>< / TextBlock>
< / DataTemplate>
< / my:DataGrid.RowHeaderTemplate>
< my:DataGrid.ColumnHeaderStyle>
< Style TargetType =my:DataGridColumnHeader>
< Setter Property =ContentTemplate>
< Setter.Value>
< DataTemplate>
< TextBlock Text ={Binding}Foreground =Blue/>
< / DataTemplate>
< /Setter.Value>
< / Setter>
< / Style>
< / my:DataGrid.ColumnHeaderStyle>

< my:DataGrid.ContextMenu>
< ContextMenu Name =cmDataGridStaysOpen =True>
< MenuItem Name =mnuViewHeader =نمایش>
< MenuItem Name =mnuHideColumnHeader =隐藏列Click =mnuHideColumn_Click/>
< MenuItem Name =mnuShowColumnHeader =显示列/>
<分隔符/>
< MenuItem Name =mnuGroupByColumn
标题=按此列分组Click =mnuGroupColumn_Click/>
< MenuItem Name =mnuClearGroups
标题=清除分组Click =mnuGroupColumn_Click/>
<分隔符/>
< MenuItem Header =Header Alignment>
< MenuItem Name =mnuHeaderCenterHeader =Center/>
< MenuItem Name =mnuHeaderLeftHeader =Left/>
< MenuItem Name =mnuHeaderRightHeader =Right/>
< / MenuItem>
< MenuItem Header =内容对齐>
< MenuItem Name =mnuContentCenterHeader =Center/>
< MenuItem Name =mnuContentLeftHeader =Left/>
< MenuItem Name =mnuContentRightHeader =Right/>
< / MenuItem>
< / MenuItem>
< / ContextMenu>
< / my:DataGrid.ContextMenu>
< / my:DataGrid>

以下代码执行绑定:



注意:我所有的列正在生成,取决于请求加载的对象:

  public static DataGridColumn CreateTextBoxWithBackgroudColumn DataColumn dataCol,string columnName)
{
DataGridTemplateColumn dgtc = new DataGridTemplateColumn();
dgtc.Header = columnName;
dgtc.HeaderStyle =(Style)(App.Current as App).FindResource(ColumnHeaderStyle);

FrameworkElementFactory cellTemplateFactory = new FrameworkElementFactory(typeof(TextBlock));
绑定dataBinding = new Binding(dataCol.ColumnName);
dataBinding.Mode = BindingMode.TwoWay;
dataBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

dataBinding.Converter = new BackGroundConverter();

cellTemplateFactory.SetBinding(TextBlock.BackgroundProperty,dataBinding);
DataTemplate cellTemplate = new DataTemplate();
cellTemplate.VisualTree = cellTemplateFactory;
cellTemplate.Seal();
dgtc.CellTemplate = cellTemplate;

return dgtc;
}

返回的DataGridColumn将被添加到我的DataGrid列。



同时可以同时使用RowVirtualization和着色吗?



谢谢。

解决方案

由于RowVirtualization对于行背景着色不能正常工作,我添加了一个列并将其着色,而不是整行。


I have a WPF application and there is a Datagrid in some pages. This datagrid needs to load 5000 rows at once (Pagination is not an option for me) and this takes ages. I set EnableRowVirtualization=True and the performance is acceptable now, but there is a problem here.In my datagrid I need to set different background colors to different rows depending on a column value (say STATUS), changing EnableRowVirtualization from False to True, caused incorrect coloring when I scroll.

----Edit----

Here is my XAML code:

<my:DataGrid Name="dgDataGrid" DockPanel.Dock="Top" AutoGenerateColumns="False"  ClipboardCopyMode="ExcludeHeader"
                     CanUserDeleteRows="True" RowHeight="20" SelectionMode="Extended"  SelectionUnit="FullRow" FontFamily="Tahoma"
                     ItemsSource="{Binding}"  VirtualizingStackPanel.VirtualizationMode="Recycling" VirtualizingStackPanel.IsVirtualizing="True"
                     EnableRowVirtualization="True" EnableColumnVirtualization="False" IsSynchronizedWithCurrentItem="True" BorderBrush="Blue"
                     RowBackground="White" HorizontalGridLinesBrush="Blue"  GridLinesVisibility="Horizontal" VerticalGridLinesBrush="Blue"
                     IsTextSearchEnabled="False" IsTabStop="True" HeadersVisibility="All" Loaded="dgDataGrid_Loaded"
                     ContextMenuOpening="dgDataGrid_ContextMenuOpening" LoadingRow="dgDataGrid_LoadingRow"
                     ScrollViewer.IsDeferredScrollingEnabled ="True">
            <my:DataGrid.Resources>

            </my:DataGrid.Resources>

            <my:DataGrid.RowHeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type my:DataGridRow}}, Path=Header}"></TextBlock>
                </DataTemplate>
            </my:DataGrid.RowHeaderTemplate>
            <my:DataGrid.ColumnHeaderStyle>
                <Style TargetType="my:DataGridColumnHeader">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <TextBlock Text="{Binding}" Foreground="Blue"/>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </my:DataGrid.ColumnHeaderStyle>

            <my:DataGrid.ContextMenu>
                <ContextMenu Name="cmDataGrid" StaysOpen="True">
                    <MenuItem Name="mnuView" Header="نمایش">
                        <MenuItem Name="mnuHideColumn"  Header="Hide Column" Click="mnuHideColumn_Click"/>
                        <MenuItem Name="mnuShowColumn" Header="Show Column"/>
                        <Separator/>
                        <MenuItem Name="mnuGroupByColumn"
                          Header="Group by this column" Click="mnuGroupColumn_Click" />
                        <MenuItem Name="mnuClearGroups"
                          Header="Clear grouping" Click="mnuGroupColumn_Click" />
                        <Separator/>
                        <MenuItem Header="Header Alignment">
                            <MenuItem Name="mnuHeaderCenter" Header="Center"/>
                            <MenuItem Name="mnuHeaderLeft" Header="Left"/>
                            <MenuItem Name="mnuHeaderRight" Header="Right"/>
                        </MenuItem>
                        <MenuItem Header="Content Alignment">
                            <MenuItem Name="mnuContentCenter" Header="Center"/>
                            <MenuItem Name="mnuContentLeft" Header="Left"/>
                            <MenuItem Name="mnuContentRight" Header="Right"/>
                        </MenuItem>
                    </MenuItem>
                </ContextMenu>
            </my:DataGrid.ContextMenu>
        </my:DataGrid>

and following codes do the bindings:

Note: all my columns are getting generated on the fly depends on the object requested to be loaded:

public static DataGridColumn CreateTextBoxWithBackgroudColumn(DataColumn dataCol, string columnName)
        {
            DataGridTemplateColumn dgtc = new DataGridTemplateColumn();
            dgtc.Header = columnName;
            dgtc.HeaderStyle = (Style)(App.Current as App).FindResource("ColumnHeaderStyle");

            FrameworkElementFactory cellTemplateFactory = new FrameworkElementFactory(typeof(TextBlock));
            Binding dataBinding = new Binding(dataCol.ColumnName);
            dataBinding.Mode = BindingMode.TwoWay;
            dataBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

            dataBinding.Converter = new BackGroundConverter();

            cellTemplateFactory.SetBinding(TextBlock.BackgroundProperty, dataBinding);
            DataTemplate cellTemplate = new DataTemplate();
            cellTemplate.VisualTree = cellTemplateFactory;
            cellTemplate.Seal();
            dgtc.CellTemplate = cellTemplate;

            return dgtc;
        }

The returned DataGridColumn will be added to my DataGrid columns.

Is that possible to have both RowVirtualization and coloring at the same time?

Thanks.

解决方案

As RowVirtualization is not working well for row background coloring, I added a column and colored it instead of whole row.

这篇关于RowVirtualization导致行的背景颜色不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 20:40