2

我正在尝试将双手柄滑块修改为通用双模型。

[更新]:XAML 代码:

<Slider x:Name="LowerSlider" DataContext="this" Template="{StaticResource simpleSlider}" Margin="10,0,0,0"
            IsEnabled="{Binding Path=IsUpperSliderEnabled}"                  
            Minimum="{Binding Path=Minimum}"
            Maximum="{Binding Path=Maximum}"
            Value="{Binding Path=LowerValue}"                
            SmallChange="{Binding Path=SmallChange}" 
            LargeChange="{Binding Path=LargeChange}" />
    <Slider x:Name="UpperSlider" DataContext="this" Template="{StaticResource simpleSlider}" Margin="10,0,0,0"
            IsEnabled="{Binding Path=IsUpperSliderEnabled}"                  
            Minimum="{Binding Path=Minimum}"
            Maximum="{Binding Path=Maximum}"
            Value="{Binding Path=UpperValue}"                
            SmallChange="{Binding Path=SmallChange}" 
            LargeChange="{Binding Path=LargeChange}" />

C#代码隐藏:

 #region Dependency Property - Minimum
    public Double Minimum
    {
        get { return (Double)GetValue(MinimumProperty); }
        set { SetValue(MinimumProperty, value); }
    }

    public static readonly DependencyProperty MinimumProperty =
        DependencyProperty.Register("Minimum", typeof(Double), typeof(DualHandleSlider), new UIPropertyMetadata(0.0));
    #endregion

    #region Dependency Property - Lower Value
    public Double LowerValue
    {
        get { return (Double)GetValue(LowerValueProperty); }
        set { SetValue(LowerValueProperty, value); }
    }

    public static readonly DependencyProperty LowerValueProperty =
        DependencyProperty.Register("LowerValue", typeof(Double), typeof(DualHandleSlider), new UIPropertyMetadata(0.0));
    #endregion

    #region Dependency Property - Upper Value
    public Double UpperValue
    {
        get { return (Double)GetValue(UpperValueProperty); }
        set { SetValue(UpperValueProperty, value); }
    }

    public static readonly DependencyProperty UpperValueProperty =
        DependencyProperty.Register("UpperValue", typeof(Double), typeof(DualHandleSlider), new UIPropertyMetadata(5.0, new PropertyChangedCallback(OnUpperValueChanged)));

    public static void OnUpperValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

    }
    #endregion

    #region Dependency Property - Maximum
    public Double Maximum
    {
        get { return (Double)GetValue(MaximumProperty); }
        set { SetValue(MaximumProperty, value); }
    }

    public static readonly DependencyProperty MaximumProperty =
        DependencyProperty.Register("Maximum", typeof(Double), typeof(DualHandleSlider), new UIPropertyMetadata(10.0, new PropertyChangedCallback(OnMaximumChanged)));

    public static void OnMaximumChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        DualHandleSlider slider = (DualHandleSlider)d;

        if (slider.IsUpperValueLockedToMax)
        {
            slider.UpperValue = (Double)e.NewValue;
        }
    }

    #endregion

    #region Dependency Property - Small Change
    public double SmallChange
    {
        get { return (double)GetValue(SmallChangeProperty); }
        set { SetValue(SmallChangeProperty, value); }
    }

    // Using a DependencyProperty as the backing store for SmallChange.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SmallChangeProperty =
        DependencyProperty.Register("SmallChange", typeof(double), typeof(DualHandleSlider),
            new UIPropertyMetadata(1.0, new PropertyChangedCallback(OnSmallChangePropertyChanged)));

    protected static void OnSmallChangePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine(e.NewValue);
    }
    #endregion

    #region Dependency Property - Large Change

    public double LargeChange
    {
        get { return (double)GetValue(LargeChangeProperty); }
        set { SetValue(LargeChangeProperty, value); }
    }

    // Using a DependencyProperty as the backing store for LargeChange.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LargeChangeProperty =
        DependencyProperty.Register("LargeChange", typeof(double), typeof(DualHandleSlider),
                new UIPropertyMetadata(1.0, new PropertyChangedCallback(OnLargeChangePropertyChanged)));

    protected static void OnLargeChangePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine(e.NewValue);
    }
    #endregion

问题是,该控件不会以任何方式对实现控件时设置的属性做出反应。即使以编程方式编辑属性也不起作用?我认为这是一个愚蠢的错误,但我已经为这段代码倾注了一段时间。有任何想法吗?

编辑:仍然没有运气!输出中没有关于绑定错误的内容。

奇怪的行为!

4

2 回答 2

2

在最新编辑之前,您已接近初始代码。关键是命名您的用户控件并绑定到用户控件DataContext如果您曾经支持嵌入式ContentControls ,这将允许自然流过。设置DataContext="this"从字面上将您的数据上下文设置为字符串 "this"

而是x:Name="ThisControl"在您的<UserControl...行中添加类似的内容,然后更新绑定以指向ElementName=ThisControl. 确保Slider.Value绑定是Mode=TwoWay(如果您不指定模式,这是默认设置):

<UserControl x:Class="NameSpace.ThisControl"
             x:Name="ThisControl">
<Grid>
    <Slider x:Name="LowerSlider"
            IsEnabled="{Binding IsUpperSliderEnabled, ElementName=ThisControl}"
            Minimum="{Binding Minimum, ElementName=ThisControl}"
            Maximum="{Binding Maximum, ElementName=ThisControl}"
            Value="{Binding LowerValue, ElementName=ThisControl}"
            SmallChange="{Binding SmallChange, ElementName=ThisControl}"
            LargeChange="{Binding LargeChange, ElementName=ThisControl}" />
    <Slider x:Name="UpperSlider"
            IsEnabled="{Binding IsUpperSliderEnabled, ElementName=ThisControl}"  
            Minimum="{Binding Minimum, ElementName=ThisControl}"
            Maximum="{Binding Maximum, ElementName=ThisControl}"
            Value="{Binding UpperValue, ElementName=ThisControl}"
            SmallChange="{Binding SmallChange, ElementName=ThisControl}" 
            LargeChange="{Binding LargeChange, ElementName=ThisControl}" />
于 2012-05-16T22:16:16.613 回答
1

您是否将控件 DataContext 属性设置为正确的值?您可以查看输出窗口以查看您可能遇到的绑定错误。

编辑:使用 ElementName=root 您将绑定指向名为 root 的东西。这样做的正常方法(恕我直言)是删除所有绑定的 ElementName 部分并设置 DataContext = this 以便能够绑定到它自己的 DP。

于 2012-05-16T21:39:00.407 回答