Styling a Custom DataGrid with Generic.xaml
Sorry about the lengthy question. I hope it's not as complicated as it was to write it :)
I'm trying to style a custom DataGrid
via Generic.xaml, and I'm finding some funny behavior. Here's what I've got:
I've created a solution with 2 projects: A controls project (class library) and a WPF Application project, called UI.
In the controls project, there's a Generic.xaml file (will be described shortly) and the following DataGrid
:
public class MyDataGrid : DataGrid
{
static MyDataGrid()
{
// Override the default style key - so in the generic.xaml,
// the style that will be used is {x:type ns:MyDataGrid}
DefaultStyleKeyProperty.
OverrideMetadata(typeof(MyDataGrid),
new FrameworkPropertyMetadata(typeof(MyDataGrid)));
}
}
In the UI project, I've created 2 grids, as such:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Controls:MyDataGrid Grid.Column="0" Margin="10" BorderBrush="Black"
BorderThickness="1" ItemsSource="{Binding Dogs}"
AutoGenerateColumns="True"
/>
<Controls:MyDataGrid Grid.Column="1" Margin="10" BorderBrush="Black"
BorderThickness="1" ItemsSource="{Binding Dogs}"
AutoGenerateColumns="True"
AlternatingRowBackground="LightBlue"
RowBackground="Orange"
AlternationCount="2"
/>
</Grid>
Note that the last grid contains local styling.
Now the generic.xaml in the controls project is as such (sorry for its length.. )
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WMDGHM.Controls">
<Style TargetType="{x:Type local:MyDataGrid}">
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="Background" Value="Pink"/> <!-- GOOD -->
<Setter Property="RowBackground" Value="Red" /> <!-- EVIL -->
<Setter Property="AlternatingRowBackground" Value="Yellow" /> <!-- EVIL -->
<!-- "#FFF4F4F4" -->
<Setter Property="HeadersVisibility" Value="Column" />
<Setter Property="GridLinesVisibility" Value="None" />
<Setter Property="SelectionMode" Value="Extended"/>
<Setter Property="RowHeight" Value="19"/>
<Setter Property="Foreground" Value="#FF000000"/>
<Setter Property="FontSize" Value="11"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="BorderBrush" Value="#FFA0A0A0"/>
<Setter Property="BorderThickness" Value="0,0,0,1"/>
<Setter Property="CanUserResizeColumns" Value="True"/>
<Setter Property="RowDetailsVisibilityMode" Value="Collapsed"/>
<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 local:MyDataGrid}">
<Border Bord开发者_如何学PythonerBrush="{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 x:Name="PART_ColumnHeadersPresenter" Grid.Column="1" 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}"/>
<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>
</ResourceDictionary>
Now, here's the funny thing: The result looks like this:
You can see both grids have a pink background (meaning, the generic.xaml gets hit) but the left grid does not get the red and yellow alternating colors as I expected it to get. The really funny thing is that Snoop shows me that these values are set correctly:
Why aren't these values being set?
Thanks in advance,
Felix.
精彩评论