通过 DataTemplate 实现可编辑的 ListBox 项后 ListBox.ItemsSource 没有更新
No update of ListBox.ItemsSource after implementing editable ListBox items via DataTemplate
我实现了可编辑的 ListBox
项目,就像在这个答案中发布的那样 在带有数据模板 (WPF) 的列表框中内联编辑 TextBlock
.
但是新值没有在我的 ListBox 的 ItemsSource
对象中更新。
这是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>
这是视图模型中的 ConfigProfiles
属性:
/// <summary>Configuration profiles that were found in the active storage path</summary>
public ObservableCollection<string> ConfigProfiles { get; private set; } = new ObservableCollection<string>();
我是不是理解错了什么?
可能是因为项目来源的类型是 ObservableCollection<string>
而不是 ObservableCollection<ProperClassImplementation>
(这是遗留原因)。
我对 WPF 比较陌生,不知道如何调试它。
May it be the reason, that the items source is of type ObservableCollection<string>
instead of ObservableCollection<ProperClassImplementation>
(which is of legacy reasons).
是的,没错。你不能修改 string
因为它是不可变的。您需要绑定到 class 的 string
属性,这意味着您需要用 ObservableCollection<ProperClassImplementation>
.
替换 ObservableCollection<string>
恐怕绑定引擎不会用新的 string
替换 ObservableCollection<string>
中的 string
,如果这正是您所希望的。
我实现了可编辑的 ListBox
项目,就像在这个答案中发布的那样 在带有数据模板 (WPF) 的列表框中内联编辑 TextBlock
.
但是新值没有在我的 ListBox 的 ItemsSource
对象中更新。
这是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>
这是视图模型中的 ConfigProfiles
属性:
/// <summary>Configuration profiles that were found in the active storage path</summary>
public ObservableCollection<string> ConfigProfiles { get; private set; } = new ObservableCollection<string>();
我是不是理解错了什么?
可能是因为项目来源的类型是 ObservableCollection<string>
而不是 ObservableCollection<ProperClassImplementation>
(这是遗留原因)。
我对 WPF 比较陌生,不知道如何调试它。
May it be the reason, that the items source is of type
ObservableCollection<string>
instead ofObservableCollection<ProperClassImplementation>
(which is of legacy reasons).
是的,没错。你不能修改 string
因为它是不可变的。您需要绑定到 class 的 string
属性,这意味着您需要用 ObservableCollection<ProperClassImplementation>
.
ObservableCollection<string>
恐怕绑定引擎不会用新的 string
替换 ObservableCollection<string>
中的 string
,如果这正是您所希望的。