Hi, I basically started today with WPF, and I'm astounded by how difficult it is to do binding. I have an array of Textboxes, in an ObservableCollection, and just want to bind that in my Listbox, so that they arrange themselves vertically. I have fiddled around with this for 3 already, can you help? I'm working in a Wpf "UserControl", not a window as so many tutorials seem to rely on.
+2
A:
In your C# code, you can do something like this:
myListBox.ItemsSource = myTextBoxesCollection;
Or in your XAML code:
<ListBox ItemsSource="{Binding MyTextBoxesCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Text}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
For the XAML, MyTextBoxesCollection needs to be a public property on your data context. One way to set the DataContext could be, in your constructor:
DataContext = this;
chrensli
2010-06-04 13:08:46
I don't think this will work. MyListBox should be ListBox, for one. And in answer to your suspicion, it does not need to be a dependency property.
Eric Mickelsen
2010-06-04 13:16:21
My original post needed work, so it is rewritten.
chrensli
2010-06-04 13:35:15
if I do this, it tells my: System.InvalidOperationException: Items collection must be empty before using ItemsSource.even though I actually do assign it before I add textboxes.
Blub
2010-06-04 13:55:25
When using an ItemsControl you must use either the Items property or the ItemsSource property, you can't use both at the same time. Make sure you aren't setting the content of the ListBox like this: <ListBox .... ... all that crap I show above ... <SomeItem /> <SomeItem /> </ListBox>And make sure you aren't adding anything to the Items collection in your code behind either.
chrensli
2010-06-04 14:00:09
This works, THANK YOU. Every other answer was wrong.
Blub
2010-06-07 05:30:46
@Blurb: Still, I question why you are creating a collection of text boxes, and then binding the Text properties of them to a ListBox to create new TextBoxes. Is this really what you want? Or do you want a collection of strings?
Brian Genisio
2010-06-08 09:31:59
+1
A:
Make MyTextBoxCollection (your ObservableCollection of textboxes) a public property of your DataContext.
<ListBox ItemsSource=”{Binding MyTextBoxCollection}”>
<ListBox.ItemTemplate>
<DataTemplate><TextBox Text=”{Binding Text}” /></DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Eric Mickelsen
2010-06-04 13:14:00
Hm, doesn't seem to work. My listbox only shows "System.Windows.DataTemplate" in a single row. I'm adding this in the UserControl constructor though:TextBox temp = new TextBox();temp.Text = "Test";collectedTodos.Add(temp);Following error:System.Windows.Data Error: 40 : BindingExpression path error: 'mycollection' property not found on 'object' ''MyControl' (Name='Window')'. BindingExpression:Path=mycollection; DataItem='MyControl' (Name='Window'); target element is 'ListBox' (Name='Test2'); target property is 'ItemsSource' (type 'IEnumerable')
Blub
2010-06-04 13:37:28
@Blub: change your ItemsSource like this `<ListBox ItemsSource=”{Binding collectedTodos}”>`. Wrap the `<DataTemplate><TextBox Text=”{Binding Text}” /></DataTemplate>` block with `<ListBox.ItemTemplate>` as in Brian's answer.
Veer
2010-06-04 13:48:26
You are missing <ListBox.ItemTemplate> between the ListBox and DataTemplate
Brian Genisio
2010-06-04 13:50:52
I still get the same error and nothing shows up in the listbox. I also get these: System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Microsoft.VisualStudio.PlatformUI.VsToolBar', AncestorLevel='1''. BindingExpression:Path=DataContext.DockLocation; DataItem=null; target element is 'VsMenuItem' (Name=''); target property is 'NoTarget' (type 'Object')
Blub
2010-06-04 14:08:04
@Blub: What do you mean? My answer is nearly identical to the accepted one.
Eric Mickelsen
2010-06-07 05:46:45
+2
A:
Having an ObservableCollection<TextBox>
is alomost always the wrong approach. You likely want an ObservableCollection<string>
instead.
Then, in your ListBox (or ItemsCollection) you have the following code:
<ListBox ItemsSource="{Binding MyStrings}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding .}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Brian Genisio
2010-06-04 13:25:40
I think you need to edit your question to format `ObservableCollection<TextBox>` and `ObservableCollection<String>` as the first line has no meaning in it.
Veer
2010-06-04 13:29:33
This does NOT work. I have tried this in every concievable way. Please create a WPF project yourself and try this.
Blub
2010-06-07 05:24:54