Стилизация пользовательской сетки данных с помощью Generic.xaml - PullRequest
2 голосов
/ 30 мая 2011

Извините за длинный вопрос. Надеюсь, это не так сложно, как написать :):

Я пытаюсь стилизовать пользовательский DataGrid через Generic.xaml, и я нахожу какое-то забавное поведение. Вот что у меня есть:

Я создал решение с двумя проектами: проектом управления (библиотекой классов) и проектом приложения WPF, который называется UI. В проекте элементов управления есть файл 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)));
    }
}

В проекте пользовательского интерфейса я создал 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

Почему эти значения не устанавливаются?

Заранее спасибо,
Felix.

...