0

I've got a ListBox that is displaying a dynamic number of TextBoxes. The user will enter text into these boxes. When the Submit button is clicked, I need to be able to access the text the user has input, should be at ListBox.Items, like so:

    //Called on Submit button click
    private void SaveAndSubmit(object sender, ExecutedRoutedEventArgs e)
    {
        var bounds = MyListBox.Items;
    }

But MyListBox.Items doesn't change after I initially set the ItemsSource, here:

    //Field declaration
    //Bounds is containing a group of strings that represent the boundaries
    //for a contour plot. The min/max values are stored at the front and back
    //of the group. However, there can be any number of dividers in between.
    public ObservableCollection<string> Bounds { get; set; }

    ...
    //Initialize Bounds in the constructor

    //Called when the selected item for DVList (an unrelated ListBox) is changed
    private void DVSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var selectedDV = DVList.SelectedItem as DVWrapper;
        if (selectedDV != null)
        {
            //Setting min/max
            Bounds[0] = selectedDV.MinValue;
            Bounds[Bounds.Count - 1] = selectedDV.MaxValue;

            MyListBox.ItemsSource = Bounds;
        }
    }

My XAML looks like this:

<Window.Resources>
    <Style x:Key="BoundsStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Grid>
                        ...
                        <TextBox/>
                    </Grid>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Focusable" Value="False"/>
    </Style>
</Window.Resources>

...

                <ListBox Name="MyListBox"
                         ItemContainerStyle="{StaticResource BoundsStyle}"/>

So when SaveAndSubmit is called, bounds ends up being what I had originally set it to in DVSelectionChanged. In other words, the listbox is not updating based on what the user has input into the textboxes contained in listbox. How can I get the updated ListBoxItems? I think my problem is similar to this one, but it's not working for me at the moment.

When I step through in the debugger, I can get individual ListBoxItems. However, their Content is empty. I'm looking into that right now.

2
  • It's hard to follow, understand - I think you should properly implement this using MVVM. Bind to ItemsSource, bind DVSelection - and handle changes in the view-model. Don't deal handle ItemsSource in code behind directly, as you'll probably get it wrong, binding, updating etc.
    – NSGaga
    Apr 24, 2013 at 17:20
  • Thank you for the response. I will try to explain better what's going on up in the question. I am working in the code-behind because this is technically a small dialog that will be shown on a certain command (from my VM). I am trying to prepare the values from the TextBoxes to be submitted. The VM doesn't care about what the user does while he's working in the dialog; he just cares about what is returned to him. So I'm preparing the data in the dialog's code-behind. I will communicate with the VM after the preparing the data in SaveAndSubmit. Or that's what I plan on at least.
    – Joseph
    Apr 24, 2013 at 17:28

1 Answer 1

0

You need to bind content of the textbox.

<TextBox/> need to change to <TextBox Content="{Binding}"/>

But follow MVVM else it will be difficult to find these errors.

3
  • TextBox doesn't have a Content, so I used the Text property. What am I binding to? When I just used "{Binding}", I received an error about two-way binding requiring a Path.
    – Joseph
    Apr 24, 2013 at 17:33
  • {Binding} means binding to the object itself. As your observablecollection is of string therefore there is no property of string that we want to bind. We need to bind to the string itself. Ok Text property is correct. Probably 2 way binding requires a property. So you need to create an object rather than string and bind the text box to property of it. Apr 24, 2013 at 18:00
  • That did it. Thank you for the answer. I am going to work with it a little more. I am still not convinced that MVVM would have solved/avoided that particular problem though.
    – Joseph
    Apr 24, 2013 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.