Register Property as DependencyProperty

末鹿安然 提交于 2020-01-11 09:59:08

问题


I have a UserControl called ChartView. There I have a Property of type ObservableCollection. I have implemented INotifyPropertyChanged in the ChartView.

The code for a ChartEntry is:

public class ChartEntry
{
   public string Description { get; set; }
   public DateTime Date { get; set; }
   public double Amount { get; set; }
}

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}"/>

I get a message in the xaml-window that I can not bind to a non-dependency-property or non-dependency-object.

I tried to register the ObservableCollection as a DependencyProperty, but with no success. I tried it with the code from WPF Tutorial

My code for the Attached-Property is

 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);
        }
    }

This also didn't work. How do I register my Property as a DependencyProperty?


回答1:


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); }
}



回答2:


You dont need AttachedProperty here. In your ChartView add the DependencyProperty like

    public static readonly DependencyProperty ChartEntriesProperty =
        DependencyProperty.Register("ChartEntries",
                                            typeof(ObservableCollection<ChartEntry>),
                                            typeof(ChartView),
                                            new FrameworkPropertyMetadata(OnChartEntriesChanged));

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

    }

Now you can bind your ChartEntries property :

 <charts:ChartView ChartEntries="{Binding PROPERTYOFYOURDATACONTEXT}"/>


来源:https://stackoverflow.com/questions/19542156/register-property-as-dependencyproperty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!