Binding DependencyProperty属性为子样式ComboBox



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

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

属性为GryphlBrush

下面的代码示例:

<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;
}
}

您应该遵守依赖项属性及其getter和setter的命名约定。此外,对属性使用nameof可以防止重命名时出现错误。

用于存储依赖属性的名称和特征的标识符字段的名称必须是您为依赖属性选择的名称,作为Register调用的一部分,由文字字符串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;
}
}

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

  • 创建一个自定义切换按钮,并添加一个依赖属性,然后将其绑定到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>
    

最新更新