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組み込みをサポートしている場合は、が自然に流れるようになりますContentControl。設定すると、データコンテキストが文字列DataContext="this"に文字通り設定されます。 "this"

代わりx:Name="ThisControl"に、<UserControl...行にのようなものを追加してから、を指すようにバインディングを更新しますElementName=ThisControlSlider.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と呼ばれるものにバインディングを転送します。これを行う通常の方法(IMHO)は、すべてのバインディングのElementName部分を削除し、DataContext=thisを設定して独自のDPにバインドできるようにすることです。

于 2012-05-16T21:39:00.407 に答える