开发者

WPF: Databinding a dictionary property

I have a UserControl with a property of type Dictionary, called HighlightedDates. The property holds a set of dates, with a tool tip string for each date, and it works fine when set from code.

I want to bind the HighlightedDates property to开发者_如何学Python a view model property, DatesWithNotes, of the same type, using this markup:

<MyControls:MyCalendar SelectedDate="{Binding SelectedDate}"
                       HighlightedDates="{Binding DatesWithNotes}" />

Unfortunately, I get this exception when I try to do that:

A 'Binding' cannot be set on the 'Value' property of type 'DictionaryEntry'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

It looks like WPF is trying to bind an individual entry, not the Dictionary itself.

As an alternative, I am looking at having the view model push each new value added to its dictionary out to the user control's dictionary, which seems awfully clumsy. And that brings me to my question: Is there a way to make this binding work? Thanks for your help.


This problem gets more interesting. Karmicpuppet attempted to reproduce the error using a simple test project--see his comment above. He didn't get the error in his test, but I got it when I ran the same test.

I have read quite a bit about problems involved in databinding to a dictionary object. In my project, I refactored to eliminate the dictionary and moved on. I haven't heard any other suggestions, so I am going to close out this question.


There appears to be a bug in the VS2010 WPF designer that can cause this error message to appear, even when the binding works just fine at runtime.


Even though this is already quite old question, the error "A 'Binding' cannot be set on the 'Value' property of type 'DictionaryEntry'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject." could be thrown just because the DependencyProperty isn't set as static.

This is something I get to fight with once in a while, just to notice that the static keyword is missing. It's so easy to go unnoticed, especially when there's the readonly keyword also.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜