C# 设置自定义控件的样式

C# 设置自定义控件的样式,c#,wpf,C#,Wpf,我自己做控制。一个继承自DataGrid,另一个继承自ContentControl。其中一个得到另一个,因此我尝试公开它们的属性,但由于我需要许多不同的控件,我想为我的控件创建一个样式(从DataGrid继承的样式),并将此控件的属性设置为我的ContentControl。我只是这样写代码,但它不工作。有人知道我做错了什么吗 <Style x:Key="CustomDataGridStyle" TargetType="{x:Type controls:CustomDataGr

我自己做控制。一个继承自
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"/>

您的样式设置了x:Key属性。这意味着默认情况下它不会应用于该类型的所有控件。您应该删除键属性以使样式成为默认样式并应用于所有CustomDataGrid控件,或者在CustomDataGrid定义中引用样式,如下所示:

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

 <controls:CustomDataGrid ... Style="{StaticResource CustomDataGridStyle}" ... />
</Window>

...

您的样式设置了x:Key属性。这意味着默认情况下它不会应用于该类型的所有控件。您应该删除键属性以使样式成为默认样式并应用于所有CustomDataGrid控件,或者在CustomDataGrid定义中引用样式,如下所示:

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

 <controls:CustomDataGrid ... Style="{StaticResource CustomDataGridStyle}" ... />
</Window>

...

我已经试过了。我还将CustomDataGrid定义为一种资源,你认为这是我的问题吗?我无法将样式分配给资源中的其他控件?创建一个没有绑定但只有简单值的setter。检查这是否有效。问题可能与您想象的不同。我尝试使用类似的
方法,但我需要的是将CustomDataGrid的属性公开到DataGridContainer这不可能:(.我的做法很糟糕。setter只是用来设置一个变量,而不是给它一个值引用。我已经尝试过了。我已经将CustomDataGrid定义为一个资源了。你认为这是我的问题吗?我无法将样式分配给资源中的其他控件?创建一个没有绑定但只有一个简单值的setter。检查此功能是否有效。问题可能与您认为的不同。我尝试使用类似的
功能,但我需要的是将CustomDataGrid的属性公开到DataGridContainer这不可能:(.我的做法很糟糕。setter只用于设置一个带有值的变量,而不用于提供引用