相关文章推荐
急躁的伏特加  ·  vue ...·  2 年前    · 
悲伤的大熊猫  ·  ios - ...·  2 年前    · 
没读研的足球  ·  Redis ...·  3 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.