开发者

Which WPF controls support data binding?

Is there a list somewhere? I spent a lot of time yesterday trying to bind an ObservableCollection<string> to a RichTextBox, then when that didn't work to a FlowDocument (which I eventually found a simple work-around for). If there isn't a list is there some intellisense trick to find out? I found it really strange th开发者_StackOverflowat FlowDocument didn't support data binding to a collection. It has a DataContext property that shows up in Intellisense so I figured there was some way to bind a collection. That coupled with the fact that supports single item binding made me certain I just couldn't find the proper property. Does everyone just eventually figure this out the hard way or did I miss something obvious?

[Edit - and the italicized text above are edits] I guess I'm such a beginner I don't even know how to ask the question properly. I have half my answer. What I really want to know, I now realize is two things.

  1. how do I tell which controls can be bound to a collection (the answer from below is any control that has an ItemsSource property)

  2. When looking at a control how do I know which of the properties are dependency properties that I can bind data to? (I do realize - just now - that when I go to browser and look at each property the summary section is mentioning which properties are dependency properties. A little laborious to click on every property in a control but I can live with that if that's the easiest way to find out).


Anything that is derived from FrameworkElement (which is every WPF control to my knowledge) supports data binding. However binding a collection requires a special type of binding. You will need to bind to a ItemsControl or use a control that has the ItemsSource DependencyProperty


All WPF controls support DataBinding, on almost every property. If you post specific examples, we'll be better able to help you.


Data binding is supported on any DependencyProperty.

  • The target property must be a dependency property. Most UIElement properties are dependency properties and most dependency properties, except read-only ones, support data binding by default. (Only DependencyObject types can define dependency properties and all UIElements derive from DependencyObject.)

In the case of a RichTextbox or FlowDocument you can achieve binding thru the Run.Text dependency property as of .Net 4.0. But keep in mind in a RichTextBox:

Binding text to a Run object contained within RichTextBox is not supported. Editing operations within the RichTextBox may cause the binding to be cleared.

You can bind the Document property as well, however, this may require some additional logic if you need the textual data contained within.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜