问题描述
我有一个名为ChartView用户控件。在那里,我有类型的ObservableCollection的属性。我已经在ChartView实施INotifyPropertyChanged的
I have a UserControl called ChartView. There I have a Property of type ObservableCollection. I have implemented INotifyPropertyChanged in the ChartView.
对于ChartEntry的代码是:
The code for a ChartEntry is:
public class ChartEntry
{
public string Description { get; set; }
public DateTime Date { get; set; }
public double Amount { get; set; }
}
现在我想用另一种观点认为这种控制和设置的的ObservableCollection通过数据绑定的ChartEntries。如果我尝试做它用:
Now I want to use this Control in another View and setting the ObservableCollection for the ChartEntries through DataBinding. If I try to just do it with:
<charts:ChartView ChartEntries="{Binding ChartEntriesSource}"/>
我得到在XAML窗口,我不能绑定到非依赖属性的消息或不依赖对象。
I get a message in the xaml-window that I can not bind to a non-dependency-property or non-dependency-object.
我试图注册的ObservableCollection为DependencyProperty的,但没有成功。
我从
I tried to register the ObservableCollection as a DependencyProperty, but with no success. I tried it with the code from WPF Tutorial
我对附加-属性代码
public static class ChartEntriesSource
{
public static readonly DependencyProperty ChartEntriesSourceProperty =
DependencyProperty.Register("ChartEntriesSource",
typeof(ChartEntry),
typeof(ChartView),
new FrameworkPropertyMetadata(OnChartEntriesChanged));
private static void OnChartEntriesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
public static void SetChartEntriesSource(ChartView chartView, ChartEntry chartEntries)
{
chartView.SetValue(ChartEntriesSourceProperty, chartEntries);
}
public static ChartEntry GetChartEntriesSource(ChartView chartView)
{
return (ChartEntry)chartView.GetValue(ChartEntriesSourceProperty);
}
}
这也没有工作。
我如何注册我的财产作为DependencyProperty的?
This also didn't work.How do I register my Property as a DependencyProperty?
推荐答案
您似乎是一个之间有些困惑 AttachedProperty
和的DependencyProperty
。忘掉你的 ChartEntriesSource
类...相反,添加此的DependencyProperty
到 ChartView
控制应该做的伎俩:
You seem to be somewhat confused between an AttachedProperty
and a DependencyProperty
. Forget about your ChartEntriesSource
class... instead, adding this DependencyProperty
into your ChartView
control should do the trick:
public static readonly DependencyProperty ChartEntriesProperty = DependencyProperty.
Register("ChartEntries", typeof(ObservableCollection<ChartEntry>), typeof(ChartView));
public ObservableCollection<ChartEntry> ChartEntries
{
get { return (ObservableCollection<ChartEntry>)GetValue(ChartEntriesProperty); }
set { SetValue(ChartEntriesProperty, value); }
}
这篇关于财产登记为的DependencyProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!