c# - 样式自定义控件

标签 c# wpf

我做了自己的控制。一个继承自 DataGrid,另一个继承自 ContentControl。其中一个获取另一个,所以我尝试公开它们的属性,但由于我需要许多不同的控件,我想为我的控件(继承自 DataGrid 的控件)创建一个 Style 并从此设置属性控制到我的 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 属性。这意味着默认情况下它不会应用于该类型的所有控件。您应该删除 Key 属性以使样式成为默认样式并应用于所有 CustomDataGrid 控件,或者在 CustomDataGrid 定义中引用样式,如下所示:

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

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

关于c# - 样式自定义控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11320777/

相关文章:

c# - 查找修改共享驱动器文件夹文件的用户

.net - 如何使用 WPF 构建一个简单的形状图表应用程序?

c# - 另一个 ViewModel 的属性不会改变

c# - 如何从同一个类中的静态函数调用公共(public)事件?

c# - 接收 Google Cloud Messaging (GCM) 推送通知到 UWP Windows 10 应用程序

c# - Visual Studio 2010 Professional - 问题单元测试 Web 服务

c# - 如何在 C# 中有效地将列表中对象的属性导出到 CSV?

C#:为什么 VS2005 中的代码编辑器选项卡之一上有一个星号?

.net - 如何避免 WPF 全屏应用程序中的闪烁?

wpf - 如何实现滚动内容但静态轴(如excel)