1

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 that 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).

Tod
  • 8,192
  • 5
  • 52
  • 93

3 Answers3

3

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

JMcCarty
  • 759
  • 5
  • 17
  • OK - This helps a lot. So I can type a and inside that type ItemsSource and if Intellisense doesn't show it then I can't bind a collection to that control. Now if I can just find an equally easy way to find out if a property is a dependency property. – Tod Jun 14 '11 at 18:46
2

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

Chris Shain
  • 50,833
  • 6
  • 93
  • 125
  • My specific problem was yesterday's attempt to bind a collection to a RichTextBox and a FlowDocument. How does everyone know that those Don't support data binding? How do i keep from wasting time in the future on other controls that don't support it. – Tod Jun 14 '11 at 18:37
  • They do support DataBinding. I'd suggest creating a new post with the exact details- what is it a collection of, what is the expected output, what code have you tried, what exceptions you have seen in the output window, etc. – Chris Shain Jun 14 '11 at 18:40
  • 1
    @Tod: In Visual-Studio 2010 you can just type any property, press F1 and you will get to the documentation page (otherwise just go there manually). If there is a `Dependency Property Information` section it's a DP, e.g. [`TextBox.Text`](http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.text.aspx) vs. [`ItemsControl.Items`](http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.items.aspx) – H.B. Jun 14 '11 at 18:41
  • thanks for sticking with me. I actually had a [separate post on the specific problem](http://stackoverflow.com/questions/6339376/how-to-display-observablecollectionstring-in-a-usercontrol) I was having but I think I have it solved. I'm not good enough at this yet to ask proper questions. I was trying to bind an ObservableCollection to a RTB or FlowDoc. Eventually found a work around (and if I could get paste to work I would be happy). Just trying to find out how to avoid problems in the future and I think I now see the light. – Tod Jun 14 '11 at 18:59
2

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.

user7116
  • 63,008
  • 17
  • 141
  • 172
  • XAML is only one way to declare bindings, it has no effect on whether you can do it or not. (removed that bit) – H.B. Jun 14 '11 at 18:38
  • @H.B. thank you sir, it was a casualty of a rewritten sentence. – user7116 Jun 14 '11 at 18:39
  • Thanks, this leads me to ask, how do I find out which properties are Dependency properties? I can hit F12 and go to the browser but I don't see any kind of special glyph or way to tell from the list of properties which ones are dependency properties. – Tod Jun 14 '11 at 18:43
  • @Tod: H.B. has an answer for you in another comment. I guess I don't understand what you're asking, because a cursory glance at the properties on the `RichTextBox` shows they all can be bound to, save the read-only ones. I think you're instead asking: how can I find out which controls can have their content bound *trivially*. In the case of an RTB, you bind through the `Document` property, which is non-trivial (hence, not useful for you). – user7116 Jun 14 '11 at 18:47
  • Yeah I didn't ask this very well. I was trying to bind an ObservableCollection to a RTB. (I'm also learning MVVM). It did seem to me that it should be trivial but I guess with RTB it's not. It was fairly trivial with a FlowDocument, but a little tricky. Even though I asked badly the answers here have helped a lot. Thanks to everyone. – Tod Jun 14 '11 at 19:03