我有一个使用DataGrid控件的WPF应用程序,供警车中的警察使用。我使用合并的字典来实现Day和Night“模式”,当您在两者之间切换程序时,调色板会发生变化。该应用程序从我的公司制造的特殊传感器收集数据,并将其显示给官员。
正在讨论中的DataGrid表现得很奇怪。当程序第一次启动时,它最初是空的。收集数据时,会将行添加到DataGrid中。当您启动该程序时,它最初处于Day模式。问题是第一行的背景不会更改为控件的夜间模式颜色。它保持白色,这是白天模式的颜色。如果您在白天模式和夜间模式之间来回切换,它将保持白色。
这不同于在那一行之后添加到DataGrid的任何行,这些行具有正确的颜色,并在颜色之间正确地来回切换。
下面是我在App.xaml中为DataGridRow类定义的样式:
<Application x:Class="MyApplication.App"
. . .>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyApplication;component/DayTime.xaml" />
<ResourceDictionary>
. . .
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="BorderBrush" Value="{DynamicResource DataBorder}" />
<Setter Property="Background" Value="{DynamicResource DataBackground}" />
<Setter Property="Foreground" Value="{DynamicResource DataForeground}" />
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="{DynamicResource DataBackground}" />
<Setter Property="BorderBrush" Value="{DynamicResource DataBorderFocused}" />
<Setter Property="Foreground" Value="{DynamicResource DataForeground}" />
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Background" Value="{DynamicResource DataBackground}" />
<Setter Property="BorderBrush" Value="{DynamicResource DataBorderFocused}" />
<Setter Property="Foreground" Value="{DynamicResource DataForeground}" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource DataBackgroundSelected}" />
<Setter Property="BorderBrush" Value="{DynamicResource DataBorderSelected}" />
<Setter Property="Foreground" Value="{DynamicResource DataForegroundSelected}" />
</Trigger>
</Style.Triggers>
</Style>
. . .
</ResourceDictionary>
</ResourceDictionary>
</ResourceDictionary>
</Application.Resources>
</Application>当我在程序上运行Snoop并向下钻取到有问题的DataGridRow时,Background属性的值是白色的(#FFFFFFFF),它的值源被设置为"DefaultStyle“。不过,这似乎不是我定义的风格,因为当我切换到day模式&回到白色时,它不会改变。我认为这是微软定义的默认样式,它根本没有使用我的样式。但仅在插入到DataGrid中的第一行上,如果它最初是空的。
对于后续行,值源列显示为"ParentTemplate“。这必须是我的风格,因为当你切换夜间模式时,背景颜色确实会正确地改变。
如何修复这个问题,使DataGrid中的每一行都是正确的?
编辑:
为了完整起见,这里是DataGrid控件使用的样式,以防它有所帮助。
<Style TargetType="{x:Type DataGrid}">
<Setter Property="Background" Value="{DynamicResource DataBackground}" />
<Setter Property="Foreground" Value="{DynamicResource TextForeground}" />
<Setter Property="BorderBrush" Value="{DynamicResource DataBorder}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected" />
<Setter Property="ScrollViewer.CanContentScroll" Value="true" />
<Setter Property="ScrollViewer.PanningMode" Value="Both" />
<Setter Property="Stylus.IsFlicksEnabled" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGrid}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="True">
<ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
<ScrollViewer.Template>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button Command="{x:Static DataGrid.SelectAllCommand}"
Focusable="false"
Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}"
Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
<DataGridColumnHeadersPresenter Grid.Column="1"
x:Name="PART_ColumnHeadersPresenter"
Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
CanContentScroll="{TemplateBinding CanContentScroll}"
Grid.ColumnSpan="2"
Grid.Row="1" />
<ScrollBar x:Name="PART_VerticalScrollBar"
Grid.Column="2"
Maximum="{TemplateBinding ScrollableHeight}"
Orientation="Vertical"
Grid.Row="1"
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
ViewportSize="{TemplateBinding ViewportHeight}"
MinWidth="45" Width="50" />
<Grid Grid.Column="1"
Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ScrollBar x:Name="PART_HorizontalScrollBar"
Grid.Column="1"
Maximum="{TemplateBinding ScrollableWidth}"
Orientation="Horizontal"
Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
ViewportSize="{TemplateBinding ViewportWidth}" />
</Grid>
</Grid>
</ControlTemplate>
</ScrollViewer.Template>
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false" />
</Trigger>
</Style.Triggers>
</Style>编辑:
作为经验,我将此成员变量添加到窗口的代码隐藏中,但出现了以下问题:
private static Style dataGridRowStyle = null;然后,我将以下代码添加到我的窗口的构造函数中:
if ( dataGridRowStyle == null ) {
dataGridRowStyle = FindResource( typeof( DataGridRow ) ) as Style;
MyGrid.RowStyle = dataGridRowStyle;
}通过这样做,我看到添加到DataGrid的每一行都具有原始的默认样式。当我将上面的代码移到Loaded事件处理程序时,也会发生这种情况。
接下来,我删除了上面的代码,并在app.xaml文件的Style定义中添加了一个x:Key属性。然后,我将此属性添加到DataGrid控件的定义中:
RowStyle={DynamicResource MyDataGridRowStyle}现在每一排都有我的风格。这很棒,但我认为使用TargetType属性声明我的样式就足以将其应用于所有行。为什么没有呢?
发布于 2013-03-27 11:58:23
在经历了无数次的挠头、心痛和网络搜索之后,我终于弄清楚了我的程序到底是怎么回事。
我的程序使用合并的字典来实现白天和晚上的模式。正如in this tutorial解释的那样,事实证明,合并后的字典是问题的原因。
修复方法是在根字典中放置一个默认样式。实际上,我的代码将资源字典中的所有模板都放在标记中。我将它们向上移动了一级,现在WPF在第一行和每一行都找到了我的默认模板。
https://stackoverflow.com/questions/15121729
复制相似问题