如何将依赖属性添加到DataGridRow WPF

时间:2014-06-08 07:51:46

标签: wpf properties datagrid dependencies

我正在使用自定义Datagrid,我想向DatagridRow添加属性。我不知道该怎么做。 我想我会创建一个自定义dataGridRow,但我不能在我的自定义DataGrid中使用它。错误讯息:

  

用于“DataGridRowEx”类型的样式无法应用于类型   “DataGridRow


这是我的自定义DataGridRow的XAML代码:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyDataGridRow">
    <Style x:Key="{x:Type local:DataGridRowEx}" TargetType="{x:Type local:DataGridRowEx}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:DataGridRowEx}">
                    <Border x:Name="DGR_Border"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="True">
                        <Border.Background>
                            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                <GradientStop Offset="0" Color="#AAFFFFAA" />
                                <GradientStop Offset="1" Color="#AAFFD455" />
                            </LinearGradientBrush>
                        </Border.Background>
                        <SelectiveScrollingGrid>
                            <SelectiveScrollingGrid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </SelectiveScrollingGrid.ColumnDefinitions>
                            <SelectiveScrollingGrid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </SelectiveScrollingGrid.RowDefinitions>
                            <DataGridCellsPresenter Grid.Column="1"
                                                    ItemsPanel="{TemplateBinding ItemsPanel}"
                                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            <DataGridDetailsPresenter Grid.Row="1"
                                                      Grid.Column="1"
                                                      SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen,
                                                                                                                     ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical},
                                                                                                                     Converter={x:Static DataGrid.RowDetailsScrollingConverter},
                                                                                                                     RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                                      Visibility="{TemplateBinding DetailsVisibility}" />
                            <DataGridRowHeader Grid.RowSpan="2"
                                               SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical"
                                               Visibility="{Binding HeadersVisibility,
                                                                    ConverterParameter={x:Static DataGridHeadersVisibility.Row},
                                                                    Converter={x:Static DataGrid.HeadersVisibilityConverter},
                                                                    RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                        </SelectiveScrollingGrid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

这是我自定义DataGrid的XAML代码

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyDataGrid"
    xmlns:myDataGridRow="clr-namespace:MyDataGridRow;assembly=MyDataGridRow">

    <LinearGradientBrush x:Key="HeaderBackgroundBrush" EndPoint="1,.6" StartPoint="1,0">
        <GradientStop Color="#8FABB5" Offset="0" />
        <GradientStop Color="#9FBEC9" Offset="0.7" />
        <GradientStop Color="#CDEFFB" Offset="1" />
    </LinearGradientBrush>

    <LinearGradientBrush
            x:Key="HeaderBackgroundOpacityMask"
            StartPoint="0,0"
            EndPoint="0,1">
        <GradientStop Color="#FFFFFFFF" Offset="0"/>
        <GradientStop Color="#FFFFFFFF" Offset="0.667"/>
        <GradientStop Color="#00FFFFFF" Offset="1"/>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="RowBackgroundSelectedBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientStop Color="#4F8CC7" Offset="0" />
        <GradientStop Color="#1C4B7C" Offset="0.7" />
        <GradientStop Color="#042D5B" Offset="1" />
    </LinearGradientBrush>
    <SolidColorBrush x:Key="DataGridHorizontalLinesBrush" Color="Black" />
    <SolidColorBrush x:Key="ForeignColorSelectTextBrush" Color="WhiteSmoke" />

    <Style x:Key="ColumnHeaderGripperStyle" TargetType="{x:Type Thumb}">
        <Setter Property="Width" Value="2"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Cursor" Value="SizeWE"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Border Padding="{TemplateBinding Padding}"
                    Background="{TemplateBinding Background}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Border Background="Transparent" BorderBrush="{TemplateBinding BorderBrush}"  BorderThickness="0" SnapsToDevicePixels="True">
                        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="3"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="DatagridColumnHeaderCustomTemplateStyle" TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="MinWidth" Value="0" />
        <Setter Property="MinHeight" Value="30" />
        <Setter Property="Foreground" Value="Navy" />
        <Setter Property="Cursor" Value="Hand" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="DataGridColumnHeader">
                    <Grid Name="Root">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Border x:Name="BackgroundBorder" BorderThickness="0,0,1,1" 
                                Background="{StaticResource HeaderBackgroundBrush}" 
                                BorderBrush="{x:Static SystemColors.ActiveBorderBrush}" 
                                Grid.ColumnSpan="2" />

                        <ContentPresenter Margin="6,3,6,3" HorizontalAlignment="Center" VerticalAlignment="Center" />
                        <Path x:Name="SortArrow" Visibility="Collapsed" Data="M0,0 L1,0 0.5,1 z" Stretch="Fill" 
                              Grid.ColumnSpan="2" Width="8" Height="6" Fill="Olive" Margin="0,0,0,2" 
                              VerticalAlignment="Bottom" RenderTransformOrigin="0.5,0.4" />
                        <Rectangle Width="1" Fill="Black" HorizontalAlignment="Right" Grid.ColumnSpan="2" />

                        <Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" 
                               Style="{StaticResource ColumnHeaderGripperStyle}"/>
                        <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" 
                               Style="{StaticResource ColumnHeaderGripperStyle}"/>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="BackgroundBorder" Property="Background" Value="#CDEFFB" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Background" TargetName="BackgroundBorder" Value="#4079B2" />
                        </Trigger>

                        <Trigger Property="SortDirection" Value="Ascending">
                            <Setter TargetName="SortArrow" Property="Visibility" Value="Visible"/>
                            <Setter TargetName="SortArrow" Property="RenderTransform">
                                <Setter.Value>
                                    <RotateTransform Angle="180"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="SortDirection" Value="Descending">
                            <Setter TargetName="SortArrow" Property="Visibility" Value="Visible"/>
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="DataGridRowStyle" TargetType="{x:Type myDataGridRow:DataGridRowEx}">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="WhiteSmoke"/>
        </Style.Resources>

        <Setter Property="MinHeight" Value="25"/>
        <Setter Property="AllowDrop" Value="True"/>
        <Setter Property="Background" Value="Transparent"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Linen"/>
            </Trigger>

            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="True"/>
                    <Condition Property="IsFocused" Value="True"/>
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="{StaticResource RowBackgroundSelectedBrush}"/>
                <Setter Property="Foreground" Value="{StaticResource ForeignColorSelectTextBrush}"/>
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="True"/>
                    <Condition Property="IsFocused" Value="False"/>
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="#AD855C"/>
                <Setter Property="Foreground" Value="{StaticResource ForeignColorSelectTextBrush}"/>
            </MultiTrigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="{x:Type local:DataGridEx}" TargetType="{x:Type local:DataGridEx}">

        <Setter Property="HeadersVisibility" Value="All" />
        <Setter Property="RowStyle" Value="{StaticResource DataGridRowStyle}" />
        <Setter Property="CellStyle" Value="{StaticResource DataGridCellStyle}" />
        <Setter Property="ColumnHeaderStyle" Value="{StaticResource DatagridColumnHeaderCustomTemplateStyle}" />
        <Setter Property="AutoGenerateColumns" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:DataGridEx}">
                    <Border x:Name="DG_FullBorder" SnapsToDevicePixels="True" Background="{TemplateBinding Background}" 
                            BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
                            Padding="{TemplateBinding Padding}">
                        <ScrollViewer x:Name="DG_ScrollViewer" Focusable="False">
                            <ScrollViewer.Template>
                                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <Button Width="{Binding Path=CellsPanelHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}}" Focusable="False"
                                                Command="{x:Static DataGrid.SelectAllCommand}">
                                            <Button.Visibility>
                                                <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}">
                                                    <Binding.ConverterParameter>
                                                        <DataGridHeadersVisibility>All</DataGridHeadersVisibility>
                                                    </Binding.ConverterParameter>
                                                </Binding>
                                            </Button.Visibility>
                                            <Button.Template>
                                                <ControlTemplate TargetType="{x:Type Button}">
                                                    <Grid>
                                                        <Rectangle Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" x:Name="Border" SnapsToDevicePixels="True"/>
                                                        <Polygon Fill="#FF000000" Stretch="Uniform" HorizontalAlignment="Right" Margin="8,8,3,3" x:Name="Arrow" VerticalAlignment="Bottom" Opacity="0.15" Points="0,10 10,10 10,0"/>
                                                    </Grid>
                                                    <ControlTemplate.Triggers>
                                                        <Trigger Property="IsMouseOver" Value="True">
                                                            <Setter Property="Stroke" TargetName="Border" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
                                                        </Trigger>
                                                        <Trigger Property="IsPressed" Value="True">
                                                            <Setter Property="Fill" TargetName="Border" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
                                                        </Trigger>
                                                        <Trigger Property="IsEnabled" Value="False">
                                                            <Setter Property="Visibility" TargetName="Arrow" Value="Collapsed"/>
                                                        </Trigger>
                                                    </ControlTemplate.Triggers>
                                                </ControlTemplate>
                                            </Button.Template>
                                        </Button>
                                        <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1">
                                            <DataGridColumnHeadersPresenter.Visibility>
                                                <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}">
                                                    <Binding.ConverterParameter>
                                                        <DataGridHeadersVisibility>Column</DataGridHeadersVisibility>
                                                    </Binding.ConverterParameter>
                                                </Binding>
                                            </DataGridColumnHeadersPresenter.Visibility>
                                        </DataGridColumnHeadersPresenter>
                                        <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Grid.ColumnSpan="2" Grid.Row="1" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" ContentTemplate="{TemplateBinding ContentTemplate}" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False"/>
                                        <ScrollBar x:Name="PART_VerticalScrollBar" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Grid.Column="2" Grid.Row="1" Orientation="Vertical" ViewportSize="{TemplateBinding ViewportHeight}" Maximum="{TemplateBinding ScrollableHeight}" Value="{Binding Path=VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/>
                                        <Grid Grid.Column="1" Grid.Row="2">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="{Binding Path=NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}}"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <ScrollBar x:Name="PART_HorizontalScrollBar" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Grid.Column="1" Orientation="Horizontal" ViewportSize="{TemplateBinding ViewportWidth}" Maximum="{TemplateBinding ScrollableWidth}" Value="{Binding Path=HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/>
                                        </Grid>
                                    </Grid>
                                </ControlTemplate>
                            </ScrollViewer.Template>
                            <Border x:Name="DG_ContentBorder" SnapsToDevicePixels="True" Background="{TemplateBinding Background}" 
                            BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
                            Padding="{TemplateBinding Padding}">
                                <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

非常感谢!

1 个答案:

答案 0 :(得分:2)

我怀疑您仍在为DataGridEx的行使用System.Windows.Controls.DataGridRow类型。要使用自己的DataGridRowEx类型的实例,必须覆盖DataGridEx类中的GetContainerForItemOverride()方法:

public class DataGridRowEx : DataGridRow
{
    // you can add any custom dependency property here
}

public class DataGridEx : DataGrid
{
    //...
    protected override DependencyObject GetContainerForItemOverride()
    {
        return new DataGridRowEx();
    }
}