【发布时间】: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>
SliderDictionaryValues 和 Slider 值都是该自定义控件中的依赖属性,并且它永远不会被设置。请帮忙。
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