问题描述
我正在使用一个自定义Datagrid,我想添加一个属性到DatagridRow。我不知道该怎么做我认为我将包装一个自定义的dataGridRow,但我不能在我的自定义DataGrid中使用它。错误消息:
这是我的自定义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:键入本地:DataGridRowEx}>
< Border x:Name =DGR_Border
BorderBrush ={TemplateBinding BorderBrush}
BorderThickness ={TemplateBinding BorderThickness}
SnapsToDevicePixels =True>
< Border.Background>
< LinearGradientBrush StartPoint =0.5,0EndPoint =0.5,1>
< GradientStop Offset =0Color =#AAFFFFAA/>
< GradientStop Offset =1Color =#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。垂直},
转换器= {x:静态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/演示文稿
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
xmlns:local =clr-namespace:MyDataGrid
xmlns:myDataGridRow = clr-namespace:MyDataGridRow; assembly = MyDataGridRow>
< LinearGradientBrush x:Key =HeaderBackgroundBrushEndPoint =1,.6StartPoint =1,0>
< GradientStop Color =#8FABB5Offset =0/>
< GradientStop Color =#9FBEC9Offset =0.7/>
< GradientStop Color =#CDEFFBOffset =1/>
< / LinearGradientBrush>
< LinearGradientBrush
x:Key =HeaderBackgroundOpacityMask
StartPoint =0,0
EndPoint =0,1>
< GradientStop Color =#FFFFFFFFOffset =0/>
< GradientStop Color =#FFFFFFFFOffset =0.667/>
< GradientStop Color =#00FFFFFFOffset =1/>
< / LinearGradientBrush>
< LinearGradientBrush x:Key =RowBackgroundSelectedBrushStartPoint =0,0EndPoint =0,1>
< GradientStop Color =#4F8CC7Offset =0/>
< GradientStop Color =#1C4B7COffset =0.7/>
< GradientStop Color =#042D5BOffset =1/>
< / LinearGradientBrush>
< SolidColorBrush x:Key =DataGridHorizontalLinesBrushColor =Black/>
< SolidColorBrush x:Key =ForeignColorSelectTextBrushColor =WhiteSmoke/>
< Style x:Key =ColumnHeaderGripperStyleTargetType ={x:Type Thumb}>
< Setter Property =WidthValue =2/>
< Setter Property =BackgroundValue =Transparent/>
< Setter Property =CursorValue =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 =DataGridCellStyleTargetType ={x:Type DataGridCell}>
< Setter Property =BackgroundValue =Transparent/>
< Setter Property =BorderBrushValue =Transparent/>
< Setter Property =BorderThicknessValue =1/>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType ={x:Type DataGridCell}>
< Border Background =TransparentBorderBrush ={TemplateBinding BorderBrush}BorderThickness =0SnapsToDevicePixels =True>
< ContentPresenter SnapsToDevicePixels ={TemplateBinding SnapsToDevicePixels}Margin =3/>
< / Border>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>
< Style x:Key =DatagridColumnHeaderCustomTemplateStyleTargetType ={x:Type DataGridColumnHeader}>
< Setter Property =SnapsToDevicePixelsValue =True/>
< Setter属性=MinWidth值=0/>
< Setter Property =MinHeightValue =30/>
< Setter Property =ForegroundValue =Navy/>
< Setter Property =CursorValue =Hand/>
< Setter属性=模板>
< Setter.Value>
< ControlTemplate TargetType =DataGridColumnHeader>
< Grid Name =Root>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =Auto/>
< /Grid.ColumnDefinitions>
< Border x:Name =BackgroundBorderBorderThickness =0,0,1,1
Background ={StaticResource HeaderBackgroundBrush}
BorderBrush ={x:Static SystemColors。 ActiveBorderBrush}
Grid.ColumnSpan =2/>
< ContentPresenter Margin =6,3,6,3HorizontalAlignment =CenterVerticalAlignment =Center/>
< Path x:Name =SortArrowVisibility =CollapsedData =M0,0 L1,0 0.5,1 zStretch =Fill
Grid.ColumnSpan =2Width =8Height =6Fill =OliveMargin =0,0,0,2
VerticalAlignment =BottomRenderTransformOrigin =0.5,0.4/>
< Rectangle Width =1Fill =BlackHorizontalAlignment =RightGrid.ColumnSpan =2/>
< Thumb x:Name =PART_LeftHeaderGripperHorizontalAlignment =Left
Style ={StaticResource ColumnHeaderGripperStyle}/>
< Thumb x:Name =PART_RightHeaderGripperHorizontalAlignment =Right
Style ={StaticResource ColumnHeaderGripperStyle}/>
< / Grid>
< ControlTemplate.Triggers>
< Trigger Property =IsMouseOverValue =true>
< Setter TargetName =BackgroundBorderProperty =BackgroundValue =#CDEFFB/>
< / Trigger>
< Trigger Property =IsPressedValue =true>
< Setter Property =BackgroundTargetName =BackgroundBorderValue =#4079B2/>
< / Trigger>
< Trigger Property =SortDirectionValue =Ascending>
< Setter TargetName =SortArrowProperty =VisibilityValue =Visible/>
< Setter TargetName =SortArrowProperty =RenderTransform>
< Setter.Value>
< RotateTransform Angle =180/>
< /Setter.Value>
< / Setter>
< / Trigger>
< Trigger Property =SortDirectionValue =Descending>
< Setter TargetName =SortArrowProperty =VisibilityValue =Visible/>
< / Trigger>
< /ControlTemplate.Triggers>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>
< Style x:Key =DataGridRowStyleTargetType ={x:键入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 =MinHeightValue =25/>
< Setter Property =AllowDropValue =True/>
< Setter Property =BackgroundValue =Transparent/>
< Style.Triggers>
< Trigger Property =IsMouseOverValue =True>
< Setter Property =BackgroundValue =Linen/>
< / Trigger>
< MultiTrigger>
< MultiTrigger.Conditions>
< Condition Property =IsSelectedValue =True/>
< Condition Property =IsFocusedValue =True/>
< /MultiTrigger.Conditions>
< Setter Property =BackgroundValue ={StaticResource RowBackgroundSelectedBrush}/>
< Setter Property =ForegroundValue ={StaticResource ForeignColorSelectTextBrush}/>
< / MultiTrigger>
< MultiTrigger>
< MultiTrigger.Conditions>
< Condition属性=IsSelectedValue =True/>
< Condition Property =IsFocusedValue =False/>
< /MultiTrigger.Conditions>
< Setter Property =BackgroundValue =#AD855C/>
< Setter Property =ForegroundValue ={StaticResource ForeignColorSelectTextBrush}/>
< / MultiTrigger>
< /Style.Triggers>
< / Style>
< Style x:Key ={x:Type local:DataGridEx}TargetType ={x:Type local:DataGridEx}>
< Setter Property =HeadersVisibilityValue =All/>
< Setter属性=RowStyleValue ={StaticResource DataGridRowStyle}/>
< Setter Property =CellStyleValue ={StaticResource DataGridCellStyle}/>
< Setter Property =ColumnHeaderStyleValue ={StaticResource DatagridColumnHeaderCustomTemplateStyle}/>
< Setter Property =AutoGenerateColumnsValue =False/>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType ={x:Type local:DataGridEx}>
< Border x:Name =DG_FullBorderSnapsToDevicePixels =TrueBackground ={TemplateBinding Background}
BorderBrush ={TemplateBinding BorderBrush}BorderThickness ={TemplateBinding BorderThickness}
Padding ={TemplateBinding Padding}>
< ScrollViewer x:Name =DG_ScrollViewerFocusable =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 =HeadersVisibilityRelativeSource ={RelativeSource FindAncestor,AncestorLevel = 1,AncestorType = {x:Type DataGrid}}>
< Binding.ConverterParameter>
< DataGridHeadersVisibility>全部< / DataGridHeadersVisibility>
< /Binding.ConverterParameter>
< / Binding>
< /Button.Visibility>
< Button.Template>
< ControlTemplate TargetType ={x:Type Button}>
< Grid>
< Rectangle Fill ={DynamicResource {x:Static SystemColors.ControlBrushKey}}x:Name =BorderSnapsToDevicePixels =True/>
<多边形填充=#FF000000Stretch =UniformHorizontalAlignment =RightMargin =8,8,3,3x:Name =ArrowVerticalAlignment =BottomOpacity =0.15 Points =0,10 10,10 10,0/>
< / Grid>
< ControlTemplate.Triggers>
< Trigger Property =IsMouseOverValue =True>
< Setter Property =StrokeTargetName =BorderValue ={DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}/>
< / Trigger>
< Trigger Property =IsPressedValue =True>
< Setter Property =FillTargetName =BorderValue ={DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}/>
< / Trigger>
< Trigger Property =IsEnabledValue =False>
< Setter Property =VisibilityTargetName =ArrowValue =Collapsed/>
< / Trigger>
< /ControlTemplate.Triggers>
< / ControlTemplate>
< /Button.Template>
< / Button>
< DataGridColumnHeadersPresenter x:Name =PART_ColumnHeadersPresenterGrid.Column =1>
< DataGridColumnHeadersPresenter.Visibility>
< Binding Path =HeadersVisibilityRelativeSource ={RelativeSource FindAncestor,AncestorLevel = 1,AncestorType = {x:Type DataGrid}}>
< Binding.ConverterParameter>
< DataGridHeadersVisibility>列< / DataGridHeadersVisibility>
< /Binding.ConverterParameter>
< / Binding>
< /DataGridColumnHeadersPresenter.Visibility>
< / DataGridColumnHeadersPresenter>
< ScrollContentPresenter x:Name =PART_ScrollContentPresenterGrid.ColumnSpan =2Grid.Row =1Content ={TemplateBinding Content}ContentStringFormat ={TemplateBinding ContentStringFormat}ContentTemplate ={TemplateBinding ContentTemplate CanContentScroll ={TemplateBinding CanContentScroll}CanHorizontallyScroll =FalseCanVerticallyScroll =False/>
< ScrollBar x:Name =PART_VerticalScrollBarVisibility ={TemplateBinding ComputedVerticalScrollBarVisibility}Grid.Column =2Grid.Row =1Orientation =VerticalViewportSize ={TemplateBinding ViewportHeight}Maximum ={TemplateBinding ScrollableHeight}Value ={Binding Path = VerticalOffset,Mode = OneWay,RelativeSource = {RelativeSource TemplatedParent}}/>
< Grid Grid.Column =1Grid.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_HorizontalScrollBarVisibility ={TemplateBinding ComputedHorizontalScrollBarVisibility}Grid.Column =1Orientation =HorizontalViewportSize ={TemplateBinding ViewportWidth}Maximum ={TemplateBinding ScrollableWidth} Value ={Binding Path = HorizontalOffset,Mode = OneWay,RelativeSource = {RelativeSource TemplatedParent}}/>
< / Grid>
< / Grid>
< / ControlTemplate>
< /ScrollViewer.Template>
< Border x:Name =DG_ContentBorderSnapsToDevicePixels =TrueBackground ={TemplateBinding Background}
BorderBrush ={TemplateBinding BorderBrush}BorderThickness ={TemplateBinding BorderThickness}
Padding ={TemplateBinding Padding}>
< ItemsPresenter SnapsToDevicePixels ={TemplateBinding SnapsToDevicePixels}/>
< / Border>
< / ScrollViewer>
< / Border>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>
< / ResourceDictionary>
非常感谢!
我怀疑你还在为DataGridEx的行使用System.Windows.Controls.DataGridRow类型。要使用您自己的DataGridRowEx类型的实例,必须在DataGridEx类中重写GetContainerForItemOverride()方法:
public class DataGridRowEx: DataGridRow
{
//您可以添加任何自定义依赖属性
}
public class DataGridEx:DataGrid
{
//。 ..
protected override DependencyObject GetContainerForItemOverride()
{
返回新的DataGridRowEx();
}
}
I'm working with a custom Datagrid and I want add a property to a DatagridRow. I don't know how can to do that.I think I will crate a custom dataGridRow but I Can't use it in my Custom DataGrid. Error message:
This's my XAML code for my custom DataGridRow:
<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>
and this is my XAML code for my custom DataGrid
<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>
Thanks so much!
I suspect you are still using System.Windows.Controls.DataGridRow type for your DataGridEx's rows. To use the instances of your own DataGridRowEx type, you must override GetContainerForItemOverride() method in your DataGridEx class:
public class DataGridRowEx : DataGridRow
{
// you can add any custom dependency property here
}
public class DataGridEx : DataGrid
{
//...
protected override DependencyObject GetContainerForItemOverride()
{
return new DataGridRowEx();
}
}
这篇关于如何将依赖属性添加到DataGridRow WPF中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!