是否更改WPF CustomControl样式中的子项?

是否更改WPF CustomControl样式中的子项?,wpf,custom-controls,controltemplate,Wpf,Custom Controls,Controltemplate,我有一个CustomControl,其中包括一个DataGrid。有一次我使用这个CustomControl时,我需要对DataGrid CellStyle的外观做一个小的更改 我现在正在做的是在XAML中复制我的整个CustomControl样式,只是为了更改CellStyle。所以我想保留我的基本单元格样式,只覆盖DataGrid单元格样式。因此,我需要以某种方式访问DataGrid,这可能吗 CustomControl(DataGridCustomControl)的简化样式 将类型为Sty

我有一个CustomControl,其中包括一个DataGrid。有一次我使用这个CustomControl时,我需要对DataGrid CellStyle的外观做一个小的更改

我现在正在做的是在XAML中复制我的整个CustomControl样式,只是为了更改CellStyle。所以我想保留我的基本单元格样式,只覆盖DataGrid单元格样式。因此,我需要以某种方式访问DataGrid,这可能吗

CustomControl(DataGridCustomControl)的简化样式


将类型为
Style
DataGridCellStyle
添加到
DataGridCustomControl
类中,并在
ControlTemplate
中绑定到它:

<ControlTemplate TargetType="{x:Type local:DataGridCustomControl}">
    <DataGrid x:Name="part_datagrid" CellStyle="{TemplateBinding DataGridCellStyle}" 
              ItemsSource ="...">
                <!-- some stuff which is always the same included here -->
    </DataGrid>
</ControlTemplate>


然后可以使用样式设置器设置
DataGridCellStyle
,就像设置任何其他依赖项属性一样。

-类似于DataGrid的CellStyle和RowStyle属性谢谢。对我来说,这是一个聪明的方法。我一定会坚持这种方法。对于其他对此感到困惑的人,我推荐ASh提到的线程(),这里还显示了不同的方法。
<Style x:Key="DataGridCustomControl1"  TargetType="{x:Type local:DataGridCustomControl}" BasedOn="{StaticResource {x:Type local:DataGridCustomControl}}">
    <Setter Property="DataGridCellStyle" Value="{StaticResource DataGridCellStyle1}"/>
</Style>
<Style x:Key="DataGridCustomControl1"  TargetType="{x:Type local:DataGridCustomControl}" BasedOn="{StaticResource {x:Type local:DataGridCustomControl}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
            <!-- Implementation missing-->
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<ControlTemplate TargetType="{x:Type local:DataGridCustomControl}">
    <DataGrid x:Name="part_datagrid" CellStyle="{TemplateBinding DataGridCellStyle}" 
              ItemsSource ="...">
                <!-- some stuff which is always the same included here -->
    </DataGrid>
</ControlTemplate>