I made my own controls. One inherits from DataGrid
and the another from ContentControl
. One of them gets the other so I try to expose their properties but as I need many different controls I want to make a Style for my control (the one that inherit from DataGrid
) and set the properties from this control to my ContentControl
. I just wrote the code like this but it does not work. Any body knows what I am doing wrong?
<Style x:Key="CustomDataGridStyle"
TargetType="{x:Type controls:CustomDataGrid}">
<Setter Property="CurrentRow"
Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DataGridContainer}}, Path=SelectedItem, Mode=TwoWay}" />
<Setter Property="CaptionVisibility"
Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DataGridContainer}}, Path=CaptionVisibility, Mode=TwoWay}" />
<Setter Property="CaptionText"
Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DataGridContainer}}, Path=CaptionText, Mode=TwoWay}" />
<Setter Property="RowValidationErrorTemplate"
Value="{StaticResource BasicRowValidationErrorTemplate}" />
<Setter Property="CurrentView"
Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DataGridContainer}}, Path=CurrentView, Mode=OneWayToSource}" />
<Setter Property="CurrentColumnHeaderText"
Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DataGridContainer}}, Path=CurrentColumnHeader, Mode=OneWayToSource}" />
<Setter Property="SelectedCellText"
Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DataGridContainer}}, Path=SelectedText, Mode=OneWayToSource}" />
<Setter Property="IsDataGridFocused"
Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:DataGridContainer}}, Path=HasFocus, Mode=OneWayToSource}" />
</Style>
And I have defined my control like this
<controls:CustomDataGrid x:Key="DataGridOne" AutoGenerateColumns="True" x:Shared="False" ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}" />
and the another one
<controls:DataGridContainer Content="{StaticResource DataGridOne}" DataContext="{Binding Products}"
x:Name="dataGridOne" SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type UserControl}},
Path=DataContext.SelectedItem, Mode=TwoWay}" CaptionVisibility="Collapsed"/>
Your style has x:Key attribute set. This means it won't apply to all controls of that type by-default. You should either remove the Key attribute to make the style default and apply to all CustomDataGrid controls, or reference Style in the CustomDataGrid definition like this:
<Window>
<Window.Resources>
<Style x:Key="CustomDataGridStyle" TargetType="{x:Type controls:CustomDataGrid}">
...
</Style>
</Window.Resources>
<controls:CustomDataGrid ... Style="{StaticResource CustomDataGridStyle}" ... />
</Window>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.