0

I implemented editable ListBox items like it is posted in this answer Inline editing TextBlock in a ListBox with Data Template (WPF) .

But the new value does not get updated in the ItemsSource object of my ListBox.

This is the XAML:

<ListBox Grid.Row="2" Name="ds_ConfigProfiles" ItemsSource="{Binding ConfigProfiles}" SelectedItem="{Binding ActiveConfigProfile}" IsSynchronizedWithCurrentItem="True" Panel.ZIndex="-1">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <!-- TODO: this is meant for allowing edit of the profile names, but the new name does not get stored back to ConfigProfiles -->
            <local:TextToggleEdit Text="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="40" Height="23" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

This is the ConfigProfiles property in the view model:

/// <summary>Configuration profiles that were found in the active storage path</summary>
public ObservableCollection<string> ConfigProfiles { get; private set; } = new ObservableCollection<string>();

Did I understand something wrong?

May it be the reason, that the items source is of type ObservableCollection<string> instead of ObservableCollection<ProperClassImplementation> (which is of legacy reasons).

I am relatively new to WPF and am out of ideas on how to debug this.

Nicolas
  • 754
  • 8
  • 22
  • Is ```Path=.``` something you changed when you pasted your code in it or do you really have the period at the binding path? – Brandon Nov 08 '18 at 15:22
  • *"how to debug this"* - check Output window for binding errors, set breakpoint and check if everything runs as expected. I am not sure how this is supposed to work (used answer author didn't bother to explain it), perhaps you post a complete [mcve] and explain it, then someone else can debug it for you. – Sinatr Nov 08 '18 at 15:26
  • @James have a look here, I had to learn this notation as well: https://stackoverflow.com/a/9391780/2477582 – Nicolas Nov 08 '18 at 15:56
  • @Sinatr Thank you for proposing what I had already have done for hours (you couldn't know this, of course :) ). But I DEFINITELY don't need s. o. else to debug my code for me - I always try to understand things as exactly as I can (though I am limited, I know). – Nicolas Nov 08 '18 at 15:58

1 Answers1

1

May it be the reason, that the items source is of type ObservableCollection<string> instead of ObservableCollection<ProperClassImplementation> (which is of legacy reasons).

Yes, exactly. You can't modify a string since it is immutable. You need to bind to a string property of a class which means that you need to replace the ObservableCollection<string> with an ObservableCollection<ProperClassImplementation>.

I am afraid the binding engine won't replace the string in the ObservableCollection<string> with a new string for you if that's what you had hoped for.

mm8
  • 163,881
  • 10
  • 57
  • 88