【问题标题】:Binding DependencyProperty for property is substyle ComboBox为属性绑定 DependencyProperty 是子样式 ComboBox
【发布时间】:2021-12-09 00:09:25
【问题描述】:

我有一个继承自 ComboBox 的 CustomComboBox 组件,其样式已复制到另一个资源字典 xaml 文件中。

问题是,我可以或如何从ComboBoxTemplate 中使用的ComboBoxToggleButton 的子样式绑定属性吗?

属性是:GryphlBrush in

下面的代码示例:

<Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
    <!-- ... -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                Border x:Name="templateRoot" SnapsToDevicePixels="true" Background="Transparent" BorderThickness="0" BorderBrush="Transparent" CornerRadius="4">
                    <Border x:Name="splitBorder" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" SnapsToDevicePixels="true" Margin="0" HorizontalAlignment="Right" BorderThickness="1" BorderBrush="Transparent">
                        <Path x:Name="arrow" Stroke="{Binding GryphlBrush}" VerticalAlignment="Center" Margin="0" HorizontalAlignment="Center" Fill="{StaticResource ComboBox.Static.Glyph}" Data="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z"/>
                    </Border>
                </Border>
                <!-- ... -->
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}">
    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="4">
        <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
            <!-- ... -->
            <ToggleButton x:Name="toggleButton" BorderBrush="Transparent" BorderThickness="0" Background="Transparent" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton}"/>
            <!-- ... -->
        </Grid>
    </Border>
    <!-- ... -->
</ControlTemplate>

<Style x:Key="BorderedComboBoxStyles" TargetType="{x:Type components:BorderedComboBox}">
    <Setter Property="Template" Value="{StaticResource ComboBoxTemplate}"/>
</Style>

在后面的代码中我只定义了依赖属性,getter 和 setter:

public class BorderedComboBox : ComboBox
{
    public static readonly DependencyProperty GryphlBrushDP = DependencyProperty.Register(
        "GryphlBrush", typeof(Brush), typeof(BorderedComboBox));
    
    private Brush _gryphlBrush;
    
    public Brush GryphlBrush
    {
        get => _gryphlBrush;
        set => _gryphlBrush = value;
    }
}

【问题讨论】:

    标签: c# wpf xaml data-binding controltemplate


    【解决方案1】:

    您应该遵守naming conventions for dependency properties 及其getter 和setter。此外,对属性使用nameof 可以防止重命名时出错。

    用于存储依赖属性名称和特征的标识符字段的名称必须是您在注册调用中为依赖属性选择的名称,并附加文字字符串Property。 [...]

    如果您未能遵循此命名模式,设计师可能无法正确报告您的属性,并且属性系统样式应用程序的某些方面可能无法按预期运行。

    因此您的依赖属性定义应如下所示:

    public class BorderedComboBox : ComboBox
    {
       public static readonly DependencyProperty GryphlBrushProperty = DependencyProperty.Register(
          nameof(GryphlBrush), typeof(Brush), typeof(BorderedComboBox));
    
       private Brush _gryphlBrush;
    
       public Brush GryphlBrush
       {
          get => _gryphlBrush;
          set => _gryphlBrush = value;
       }
    }
    

    如果你想在ToggleButton控件模板中绑定GryphlBrush属性,你可以:

    • 创建一个自定义切换按钮并向其添加依赖属性,然后将其绑定到您的BorderedComboBox 上的GryphlBrush 属性。

    • 如果切换按钮仅在组合框中的此上下文中使用,则将RelativeSource 与AncestorType 绑定,因为BorderedComboBox 是切换按钮的父级。

      <Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
         <!--...-->
         <Setter Property="Template">
            <Setter.Value>
               <ControlTemplate TargetType="{x:Type ToggleButton}">
                  <Border x:Name="templateRoot" SnapsToDevicePixels="true" Background="Transparent" BorderThickness="0" BorderBrush="Transparent" CornerRadius="4">
                  <Border x:Name="splitBorder" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" SnapsToDevicePixels="true" Margin="0" HorizontalAlignment="Right" BorderThickness="1" BorderBrush="Transparent">
                     <!--<Path x:Name="arrow" Stroke="{Binding GryphlBrush, RelativeSource={RelativeSource AncestorType={x:Type local:BorderedComboBox}}}" VerticalAlignment="Center" Margin="0" HorizontalAlignment="Center" Fill="{StaticResource ComboBox.Static.Glyph}" Data="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z"/>-->
                     <Path x:Name="arrow" Stroke="{Binding GryphlBrush, RelativeSource={RelativeSource AncestorType={x:Type local:BorderedComboBox}}}" VerticalAlignment="Center" Margin="0" HorizontalAlignment="Center" Data="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z"/>
                  </Border>
                  </Border>
                  <!--...-->
               </ControlTemplate>
            </Setter.Value>
         </Setter>
      </Style>
      

    【讨论】:

      猜你喜欢
      • 2012-01-31
      • 2020-12-29
      • 2014-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      相关资源
      最近更新 更多