【问题标题】:Binding value of one dependency property to another将一个依赖属性的值绑定到另一个
【发布时间】:2017-08-24 22:06:29
【问题描述】:

我创建了一个派生自 Slider 的自定义控件,并在另一个用户控件中调用该控件,如下所示

<Custom:CustomSlider
            x:Name="CustomSlider"
            Minimum="0"
            Value="{Binding SliderCurrentValue,Mode=TwoWay}"
            SliderDictionaryValues="{Binding CustomSlider.DictionaryToBrSlider,Mode=TwoWay}"
            SliderValues="{Binding SliderCurrentValue,Mode=TwoWay}"></Custom:CustomSlider>

SliderDictionaryValuesSlider 值都是该自定义控件中的依赖属性,并且它永远不会被设置。请帮忙。

DictionaryToBrSlider 是一个依赖属性,在我调用自定义Slider 控件的地方后面的代码中如下所示

public static readonly DependencyProperty DictionaryToBRSliderProperty =
        DependencyProperty.Register("DictionaryToBrSlider", typeof(Dictionary<string, double>), typeof(BRSliderUserControl),new PropertyMetadata(null,DictionaryChanged));




public Dictionary<string, double> DictionaryToBrSlider
    {
        get { return (Dictionary<string, double>)GetValue(DictionaryToBRSliderProperty); }
        set { SetValue(DictionaryToBRSliderProperty, value); }
    }


    private static void DictionaryChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
     //The break point hits here fine,.
    }

这是我的自定义滑块类。

public class CustomSlider : Slider
    {

        public Dictionary<string, double> SliderDictionaryValues
        {
            get { return (Dictionary<string, double>)GetValue(SliderValuesDictionaryProperty); }
            set { SetValue(SliderValuesDictionaryProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SliderValues.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SliderValuesDictionaryProperty =
            DependencyProperty.Register("SliderDictionaryValues", typeof(Dictionary<string, double>), typeof(CustomSlider), 
                new PropertyMetadata(null, OnSliderDictionaryPropertyChanged));

        private static void OnSliderDictionaryPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        { //BREAKPOINT NEVER HITS THIS LINE
            if (e.OldValue != e.NewValue && e.NewValue != null)
            {
                if (CustomTickBar.FontTextList == null)
                {
                    CustomTickBar.FontTextList = new List<string>();
                }
                Dictionary<string, double> NewValues = e.NewValue as Dictionary<string, double>;
                foreach(var item in NewValues)
                {
                    CustomTickBar.FontTextList.Add(item.Key);
                }
            }
        }

        public static readonly DependencyProperty SliderValuesProperty =
            DependencyProperty.Register("SliderValues", typeof(int), typeof(CustomSlider),
                new PropertyMetadata(0, OnSliderValueChanged));



        public int SliderValues
        {
            get
            {
                return (int)GetValue(SliderValuesProperty);
            }
            set
            {
                SetValue(SliderValuesProperty, value);
            }
        }

        private static void OnSliderValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }

        //public double CurrentSelectedValue
        //{
        //  get { return SliderValues.ElementAt(1).Value; }
        //}
    }

【问题讨论】:

  • 为什么要投反对票。需要解释一下吗?
  • 可能你的绑定不正确。您是否检查了输出控制台的绑定错误?第一个代码 sn-p 中那些绑定的 DataContext 是什么?您能否详细说明“我调用自定义滑块控件的位置背后的代码”?这句话没有多大意义。

标签: c# wpf dependency-properties


【解决方案1】:

将控件的DataContext设置为父UserControl

<Custom:CustomSlider
            x:Name="CustomSlider"
            DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}"
            Minimum="0"
            Value="{Binding SliderCurrentValue,Mode=TwoWay}"
            SliderDictionaryValues="{Binding DictionaryToBrSlider, Mode=TwoWay}"
            SliderValues="{Binding SliderCurrentValue, Mode=TwoWay}"></Custom:CustomSlider>

【讨论】:

【解决方案2】:
SliderDictionaryValues="{Binding ElementName=CustomSlider, Path=DictionaryToBrSlider, Mode=TwoWay}"

除非 DataContext 是从其他地方继承的,否则绑定的源对象没有被设置。与直觉相反,默认绑定源不是对象本身。没有默认值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 2010-11-14
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多