Я сделал свой собственный контроль. Один наследуется от DataGrid
, а другой от ContentControl
. Один из них получает другой, поэтому я пытаюсь разоблачить их свойства, но поскольку мне нужно много разных элементов управления, я хочу создать стиль для своего элемента управления (тот, который наследуется от DataGrid
) и установить свойства этого элемента управления на мой ContentControl
. Я просто написал код, как это, но он не работает. Любое тело знает, что я делаю неправильно?Стильный пользовательский контроль
<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>
И я определил свой контроль, как этот
<controls:CustomDataGrid x:Key="DataGridOne" AutoGenerateColumns="True" x:Shared="False" ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}" />
и другой один
<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"/>
Я уже пробовал это. И я определил свой CustomDataGrid также как ресурс, как вы думаете, это моя проблема? Я не могу назначить стиль другому элементу управления внутри моего ресурса? – Nandhi
Создайте сеттер без привязки, но простое значение. Проверьте, работает ли это. Проблема может быть иной, чем вы думаете. –
Я пытаюсь что-то вроде этого '' '' и работает , но мне нужно выставить свойства из CustomDataGrid в DataGridContainer – Nandhi