我有一个WPF MVVM应用程序。在主WPF窗口中,我有一个WPF Datagrid工具包。 此数据网格包含一些列。根据某些条件(视图模型上绑定的属性),颜色将应用于整个行。 此外,还有两个特殊列。这两列的类型为DataGridTextColumn,称为Date1和Date2。 如果Date1值大于Date2值,则Date1和Date2单元格应均为橙红色。 分别绑定到MyDate1和MyDate2视图模型属性的Date1和Date2属于DateTime。
我的问题是: 首先,当datagrid加载数据时,行和Date1和Date2列被正确着色。 由于Date1>,一些行显示为Date1,Date2显示为橙红色。 DATE2。 然后,如果我使用垂直滚动条向下滚动数据网格,然后我再次向上滚动到第一个数据网格行,我注意到现在更多行(Date1和Date2列) 尽管Date1< = Date2。
,它仍然显示为橙红色发生了什么事?
查看:
<Window x:Name="MainWindow" x:Class="My.Apps.WPF.TestApp.wMain"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:classes="clr-namespace:My.Apps.WPF.TestApp.Classes"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
WindowState="Maximized">
<Grid x:Name="MyGrid" HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<my:DataGrid Grid.Row="1" Name="MyDg"
AutoGenerateColumns="False"
ItemsSource="{Binding Path=MyListOfItems}"
SelectedItem="{Binding Path=MySelectedItem}"
VerticalAlignment="Stretch" IsReadOnly="True"
SelectionMode="Single" ColumnWidth="*"
SelectionChanged="MyDg_SelectionChanged"
Width="{Binding Path=ActualWidth, ElementName=MyGrid}">
<my:DataGrid.Resources>
<classes:BindingProxy x:Key="proxy" Data="{Binding}" />
<Style x:Key="MyDataGridCellStyle" TargetType="{x:Type my:DataGridCell}" BasedOn="{StaticResource {x:Type my:DataGridCell}}">
<!-- If Date1 > Date2 apply background color -->
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource CellDateColorConverter}">
<Binding Path="MyDate1"/> <!-- Date1 value -->
<Binding Path="MyDate2"/> <!-- Date2 value -->
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</my:DataGrid.Resources>
<my:DataGrid.RowStyle>
<!-- Style for entire datagrid row -->
<Style TargetType="my:DataGridRow">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=GroupID, Converter={StaticResource IdGroupConverter}}" Value="True" />
<Condition Binding="{Binding Path=MyFlag}" Value="0" />
<Condition Binding="{Binding Path=StatusFilter, Converter={StaticResource StatusFilterConverter}}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="#FFAC62"></Setter>
</MultiDataTrigger>
<DataTrigger Binding="{Binding Path=MyFlag}}" Value="1">
<Setter Property="Background" Value="{Binding DeniedColor}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=MyFlag}" Value="2">
<Setter Property="Background" Value="{Binding PendingColor}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</my:DataGrid.RowStyle>
<my:DataGrid.Columns>
<my:DataGridTextColumn x:Name="Date1"
CellStyle="{StaticResource MyDataGridCellStyle}"
Binding="{Binding Path=MyDate1, StringFormat=\{0:dd/MM/yyyy\}}"
Header="Date 1"
Width="{Binding DataComandaColWidth, Source={StaticResource DO}, Mode=TwoWay}"
HeaderStyle="{DynamicResource CenterGridHeaderStyle}">
<my:DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="Margin" Value="5 0"/>
</Style>
</my:DataGridTextColumn.ElementStyle>
</my:DataGridTextColumn>
<my:DataGridTextColumn x:Name="Date2"
CellStyle="{StaticResource MyDataGridCellStyle}"
Binding="{Binding Path=MyDate2, StringFormat=\{0:dd/MM/yyyy\}}"
Header="Date 2"
Width="auto"
HeaderStyle="{DynamicResource CenterGridHeaderStyle}">
<my:DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="Margin" Value="5 0"/>
</Style>
</my:DataGridTextColumn.ElementStyle>
</my:DataGridTextColumn>
</my:DataGrid.Columns>
</my:DataGrid>
</Grid>
</Window>
类:
namespace My.Apps.WPF.TestApp.Classes
{
public class CellDateColorConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (values[0] is DateTime && values[1] is DateTime)
{
DateTime date1 = (DateTime)values[0];
DateTime date2 = (DateTime)values[1];
if (date1.Date > date2.Date)
{
return System.Windows.Media.Brushes.OrangeRed;
}
}
return System.Windows.Data.Binding.DoNothing;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException("CellDateColorConverter is a OneWay converter.");
}
}
public class BindingProxy : Freezable
{
#region Overrides of Freezable
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
#endregion
public object Data
{
get { return (object)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
// Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}
}
答案 0 :(得分:0)
我通过将EnableRowVirtualization设置为False来解决。默认设置为True。默认情况下,属性EnableColumnVirtualization设置为False。
似乎使用虚拟化单元格会影响.NET 3.5中的数据网格样式。错误?如果是这样,我不知道它是否已在NET 4.0及更高版本中得到纠正。
无论如何,我很担心,因为这意味着我无法使用虚拟化细胞...所以如果有人有任何其他解决方案,请在这里分享。
其他用户遇到了同样的问题,请参阅here
<强>已更新强>:
此外,如果您想将EnableRowVirtualization保留为True,只需通过以下方式更改虚拟化模式即可解决此问题:
<DataGrid VirtualizingStackPanel.VirtualizationMode="Standard" />
问题是defaut的VirtualizationMode设置为Recycling,因此在重用容器时滚动时会导致错误的行着色。正如Eirik在他的回答中所解释的那样,here解释了这一点。