Arsen Zahray
Arsen Zahray

Reputation: 25287

Enable editing ListBox items in WPF

I'm writing a tag control, based on the listbox.

It is displaying the ListBox items using following template:

      <Setter Property="ItemTemplate">
            <Setter.Value>
              <DataTemplate>
                <local:TagControl Text="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Remove="RemoveItem" />
              </DataTemplate>
            </Setter.Value>
        </Setter>

I've noticed that when I update TagControl's text, the original item in the ListBox does not get updated. I'm using ObservableCollection<string> as items source.

TagControl implements INotifyPropertyChanged and calls the event.

What am I doing wrong?

Upvotes: 0

Views: 1131

Answers (1)

Phil
Phil

Reputation: 42991

I've reproduced your problem and can offer a solution. The ObservableCollection<string> is enumerated using IEnumerable which is read-only.

If you replace the ObservableCollection<string> with ObservableCollection<DataItem> where

public class DataItem
{
    public string Name{get;set;} 
}

and then bind to Name in your DataTemplate, the enumerated DataItem is read-only, but the Name property is read-write and will be updated when you edit the text in the list item.

Upvotes: 1

Related Questions