一个依赖属性的绑定值到另一个

时间:2017-08-24 22:06:29

标签: c# wpf dependency-properties

我创建了一个源自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; }
        //}
    }

2 个答案:

答案 0 :(得分:0)

SliderDictionaryValues="{Binding ElementName=CustomSlider, Path=DictionaryToBrSlider, Mode=TwoWay}"

除非DataContext是从其他地方继承的,否则未设置绑定的源对象。由于这是违反直觉的,因此默认绑定源不是对象本身。没有默认值。

答案 1 :(得分:0)

将控件的 <td onKeyPress='return submitEnter(event)' ></td> function submitEnter(e) { if((e && e.keyCode == 13) || e == 0) { // enter handled here } } 设置为父DataContext

UserControl
相关问题