Maddy
Maddy

Reputation: 590

How to bind a List to a Textbox in an ItemsControl

I want to add some strings to a List (via button) and show it in an ItemsSource, but it doesn't work. Thats my code:

XAML:

<ItemsControl  ItemsSource="{Binding ListInfos, Mode=OneWay}">
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Grid>
            <TextBox Text="{Binding Path=., Mode=OneWay}" />
            ...

VIEW-MODEL:

private List<String> _listInfos = new List<String>();

public List<String> ListInfos
{
    get { return _listInfos; }
    set
    {
        _listInfos = value;
        NotifyPropertyChanged("ListInfos");
    }
}

public void AddStringButton()
{
    ListInfos.Add("test");
}

The AddStringButton method works, but the property (set) of ListInfos doesn't fire and die GUI doesn't show the string test.

Upvotes: 0

Views: 733

Answers (1)

user2920518
user2920518

Reputation: 318

As dkozl said, you would need to use the ObservableCollection class, which provides notifications when the contents of the list changes. The only way the List PropertyChanged event will fire is if you set it to a new list instance.

Upvotes: 1

Related Questions