【问题标题】:Styling custom control样式化自定义控件
【发布时间】:2023-04-04 07:40:01
【问题描述】:

我自己制作了控件。一个继承自DataGrid,另一个继承自ContentControl。其中一个得到另一个,所以我尝试公开它们的属性,但由于我需要许多不同的控件,所以我想为我的控件(继承自 DataGrid 的控件)创建一个样式,并将该控件的属性设置为我的 @987654324 @。我刚刚写了这样的代码,但它不起作用。任何人都知道我做错了什么?

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

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    您的样式设置了 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>
    

    【讨论】:

    • 我已经尝试过了。而且我也将我的 CustomDataGrid 定义为一个资源,你认为这是我的问题吗?我无法将样式分配给资源中的其他控件?
    • 创建一个没有绑定但有一个简单值的设置器。检查这是否有效。问题可能与您想象的不同。
    • 我尝试使用类似 &lt;Style x:Key="CustomDataGridStyle" TargetType="{x:Type controls:RaceUIDataGrid}"&gt; &lt;Setter Property="IsDataGridReadOnly" Value="True"/&gt; &lt;/Style&gt; 的东西并且可以工作,但我需要将属性从 CustomDataGrid 公开到 DataGridContainer
    • 这是不可能的 :(。我的做法很糟糕。setter 只是为了设置一个带有值的变量而不是给它引用
    猜你喜欢
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 2016-03-21
    • 2019-12-17
    • 2012-03-26
    相关资源
    最近更新 更多