简体   繁体   English

绑定到滑块控件样式的依赖项属性不起作用

[英]Binding to dependency property in slider control style not working

All the default property bindings are working fine however my custom properties are using the fallback values. 所有默认属性绑定都可以正常工作,但是我的自定义属性正在使用后备值。 Specifically ColorThumb and ColorThumbBorder. 特别是ColorThumb和ColorThumbBorder。 As their names imply, these 2 properties color the respective parts of the slider's thumb using a brush. 顾名思义,这两个属性使用画笔为滑块拇指的各个部分着色。

I've had no problems in the past getting my styles to use custom properties for other controls. 过去让我的样式将自定义属性用于其他控件没有任何问题。 Is there something unique about the slider control? 滑块控件有什么独特之处吗?

Style Xaml 风格Xaml

<Style TargetType="{x:Type local:DMSlider}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Slider}">
                <Border>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <TickBar x:Name="TopTick" Visibility="{TemplateBinding TickPlacement}" Fill="{TemplateBinding Foreground}" Placement="Top" Height="4" Grid.Row="0"/>
                        <TickBar x:Name="BottomTick" Visibility="{TemplateBinding TickPlacement}" Fill="{TemplateBinding Foreground}" Placement="Bottom" Height="4" Grid.Row="0"/>
                        <Border x:Name="TrackBackground" BorderThickness="1" CornerRadius="0" Margin="5,0" VerticalAlignment="Center" Height="4.0" Grid.Row="1"
                                Background="{Binding Background, RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                BorderBrush="{Binding BorderBrush, RelativeSource={RelativeSource Mode=TemplatedParent}}">
                            <Canvas Margin="-6,-1">
                                <Rectangle Visibility="Hidden" x:Name="PART_SelectionRange" Height="4.0" StrokeThickness="1.0"/>
                            </Canvas>
                        </Border>
                        <Track x:Name="PART_Track" Grid.Row="1">
                            <Track.DecreaseRepeatButton>
                                <RepeatButton Command="{x:Static Slider.DecreaseLarge}" Style="{StaticResource DMSliderRepeatButton}"/>
                            </Track.DecreaseRepeatButton>
                            <Track.IncreaseRepeatButton>
                                <RepeatButton Command="{x:Static Slider.IncreaseLarge}" Style="{StaticResource DMSliderRepeatButton}"/>
                            </Track.IncreaseRepeatButton>
                            <Track.Thumb>
                                <Thumb x:Name="Thumb">
                                    <Thumb.Style>
                                        <Style TargetType="{x:Type Thumb}">
                                            <Setter Property="SnapsToDevicePixels" Value="true" />
                                            <Setter Property="OverridesDefaultStyle" Value="true" />
                                            <Setter Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate TargetType="{x:Type Thumb}">
                                                        <Border x:Name="gripBorder" BorderThickness="1"
                                                                Background="{Binding ColorThumb, 
                                                                            RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, FallbackValue=Red}"
                                                                BorderBrush="{Binding ColorThumbBorder, 
                                                                            RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, FallbackValue=Green}" >
                                                            <Rectangle x:Name="grip" Height="15" Width="10" />
                                                        </Border>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </Thumb.Style>
                                </Thumb>
                            </Track.Thumb>
                        </Track>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Properties CS 属性CS

public partial class DMSlider : Slider
{
    public static readonly DependencyProperty ColorSliderLeftProperty =
        DependencyProperty.Register("ColorSliderLeft", typeof(Brush), typeof(DMSlider));

    public static readonly DependencyProperty ColorSliderRightProperty =
        DependencyProperty.Register("ColorSliderRight", typeof(Brush), typeof(DMSlider));

    public static readonly DependencyProperty ColorSliderThumbProperty =
        DependencyProperty.Register("ColorThumb", typeof(Brush), typeof(DMSlider), new FrameworkPropertyMetadata(Brushes.Orange));

    public static readonly DependencyProperty ColorSliderThumbHoverProperty =
        DependencyProperty.Register("ColorThumbHover", typeof(Brush), typeof(DMSlider));

    public static readonly DependencyProperty ColorSliderThumbBorderProperty =
        DependencyProperty.Register("ColorThumbBorder", typeof(Brush), typeof(DMSlider));

    public Brush ColorSliderLeft
    {
        get { return (Brush)GetValue(ColorSliderLeftProperty); }
        set { SetValue(ColorSliderLeftProperty, value); }
    }

    public Brush ColorSliderRight
    {
        get { return (Brush)GetValue(ColorSliderRightProperty); }
        set { SetValue(ColorSliderRightProperty, value); }
    }

    public Brush ColorThumb
    {
        get { return (Brush)GetValue(ColorSliderThumbProperty); }
        set { SetValue(ColorSliderThumbProperty, value); }
    }

    public Brush ColorThumbHover
    {
        get { return (Brush)GetValue(ColorSliderThumbHoverProperty); }
        set { SetValue(ColorSliderThumbHoverProperty, value); }
    }

    public Brush ColorThumbBorder
    {
        get { return (Brush)GetValue(ColorSliderThumbProperty); }
        set { SetValue(ColorSliderThumbBorderProperty, value); }
    }
}

Thanks to @Clemens for showing me what I was doing wrong. 感谢@Clemens向我展示我做错了什么。 I needed to reference the slider not the the thumb. 我需要引用滑块而不是拇指。

I should have been targeting an ancestor like so: 我应该像这样瞄准一个祖先:

Background="{Binding ColorThumb, RelativeSource={RelativeSource AncestorType=local:DMSlider}, Mode=TwoWay, FallbackValue=Red}"
BorderBrush="{Binding ColorThumbBorder, RelativeSource={RelativeSource AncestorType=local:DMSlider}, Mode=TwoWay, FallbackValue=Green}"

Full thumb style 全拇指样式

<Thumb x:Name="Thumb">
<Thumb.Style>
    <Style TargetType="{x:Type Thumb}">
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Border x:Name="gripBorder" BorderThickness="1"
                            Background="{Binding ColorThumb, 
                                        RelativeSource={RelativeSource AncestorType=local:DMSlider}, Mode=TwoWay, FallbackValue=Red}"
                            BorderBrush="{Binding ColorThumbBorder, 
                                        RelativeSource={RelativeSource AncestorType=local:DMSlider}, Mode=TwoWay, FallbackValue=Green}">
                        <Rectangle x:Name="grip" Height="15" Width="10" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Thumb.Style>

您的ColorThumbBorder属性在其吸气剂中使用ColorSliderThumbProperty。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM