C# 将一个依赖项属性的值绑定到另一个依赖项属性

C# 将一个依赖项属性的值绑定到另一个依赖项属性,c#,wpf,dependency-properties,C#,Wpf,Dependency Properties,我创建了一个从Slider派生的自定义控件,并在另一个用户控件中调用该控件,如下所示 <Custom:CustomSlider x:Name="CustomSlider" Minimum="0" Value="{Binding SliderCurrentValue,Mode=TwoWay}" SliderDictionaryValues="{Binding CustomSlider.Dictio

我创建了一个从
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>
这是我的自定义滑块类

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; }
        //}
    }
公共类自定义滑块:滑块
{
公共词典幻灯片字典值
{
get{return(Dictionary)GetValue(SliderValuesDictionaryProperty);}
set{SetValue(SliderValuesDictionaryProperty,value);}
}
//使用DependencyProperty作为SliderValue的后备存储。这将启用动画、样式、绑定等。。。
公共静态只读从属属性SliderValuesDictionalProperty=
DependencyProperty.Register(“SliderDictionaryValues”、typeof(字典)、typeof(CustomSlider),
新的PropertyMetadata(null,OnSlideDictionaryPropertyChanged));
LiderDictionaryPropertyChanged上的私有静态无效(DependencyObject d、DependencyPropertyChangedEventArgs e)
{//断点从未命中此行
if(e.OldValue!=e.NewValue&&e.NewValue!=null)
{
if(CustomTickBar.fontextlist==null)
{
CustomTickBar.FontTextList=新列表();
}
字典NewValues=e.NewValue作为字典;
foreach(NewValues中的var项)
{
CustomTickBar.fontextlist.Add(item.Key);
}
}
}
公共静态只读从属属性SliderValuesProperty=
DependencyProperty.Register(“SliderValues”、typeof(int)、typeof(CustomSlider),
新的PropertyMetadata(0,OnSliderValueChanged));
公共int SliderValues
{
得到
{
返回(int)GetValue(SliderValuesProperty);
}
设置
{
设置值(SliderValuesProperty,value);
}
}
SliderValueChanged上的私有静态无效(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
}
//公共双CurrentSelectedValue
//{
//获取{return SliderValues.ElementAt(1.Value;}
//}
}

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

将控件的
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>


为什么投反对票。想解释一下吗?也许你的绑定不正确。是否检查了输出控制台的绑定错误?在第一个代码段中,这些绑定的DataContext是什么?你能详细说明一下“我调用自定义滑块控件的地方的代码”吗?这句话没有多大意义,因为每个人都认为数据上下文是问题所在。让我试着设置它。@nikhil:请记住投票选出有用的答案:
SliderDictionaryValues="{Binding ElementName=CustomSlider, Path=DictionaryToBrSlider, Mode=TwoWay}"
<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>