2

私は自分でコントロールを作りました。1つはから継承しDataGrid、もう1つはから継承しContentControlます。それらの1つがもう一方を取得するので、それらのプロパティを公開しようとしますが、多くの異なるコントロールが必要なので、コントロール(から継承するもの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}" />

ともう1つ

<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"/>
4

1 に答える 1

0

スタイルには x:Key 属性が設定されています。これは、デフォルトでそのタイプのすべてのコントロールに適用されるわけではないことを意味します。Key 属性を削除してスタイルをデフォルトにし、すべての CustomDataGrid コントロールに適用するか、次のように CustomDataGrid 定義で Style を参照する必要があります。

<Window>
  <Window.Resources>
    <Style x:Key="CustomDataGridStyle" TargetType="{x:Type controls:CustomDataGrid}">
     ...
    </Style>
  </Window.Resources>

 <controls:CustomDataGrid ... Style="{StaticResource CustomDataGridStyle}" ... />
</Window>
于 2012-07-05T18:16:02.623 に答える