使用Generic.xaml为自定义DataGrid设置样式

时间:2011-05-30 12:05:50

标签: wpf datagrid custom-controls styles generic.xaml

对这个冗长的问题感到抱歉。我希望它不像写它那么复杂:)

我正在尝试通过Generic.xaml设置自定义DataGrid的样式,我发现了一些有趣的行为。这就是我所拥有的:

我已经创建了一个包含2个项目的解决方案:一个控件项目(类库)和一个名为UI的WPF应用程序项目。 在控件项目中,有一个Generic.xaml文件(将在稍后描述)和以下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)));
    }
}

在UI项目中,我创建了2个网格,如下:

<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>

请注意,最后一个网格包含本地样式。

现在控件项目中的generic.xaml就是这样(对不起它的长度......)

<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 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 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>

现在,这是有趣的事情:结果如下:

http://i.stack.imgur.com/4eTmO.jpg

你可以看到两个网格都有一个粉红色的背景(意思是,generic.xaml被击中),但是左边的网格没有像我预期的那样得到红色和黄色的交替颜色。非常有趣的是Snoop告诉我这些值设置正确:

http://i.stack.imgur.com/z9wcU.jpg

为什么不设置这些值?

提前致谢,
菲利克斯。

0 个答案:

没有答案