tags:

views:

111

answers:

3

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
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
My original post needed work, so it is rewritten.
chrensli
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
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
This works, THANK YOU. Every other answer was wrong.
Blub
@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
+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
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
@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
You are missing <ListBox.ItemTemplate> between the ListBox and DataTemplate
Brian Genisio
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
@Brian: D'oh! Good call.
Eric Mickelsen
This does NOT work. Two-way binding requires Path or XPath.
Blub
@Blub: What do you mean? My answer is nearly identical to the accepted one.
Eric Mickelsen
+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
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
This does NOT work. I have tried this in every concievable way. Please create a WPF project yourself and try this.
Blub
Sorry... I missed the '.' in the {Binding .}
Brian Genisio