代码之家  ›  专栏  ›  技术社区  ›  Nicolas

通过DataTemplate实现可编辑的ListBox项后,不会更新ListBox.ItemsSource

  •  0
  • Nicolas  · 技术社区  · 6 年前

    我实现了可编辑 ListBox 像这样的项目是张贴在这个答案 Inline editing TextBlock in a ListBox with Data Template (WPF)

    但新值不会在 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>

    1 回复  |  直到 6 年前
        1
  •  1
  •   mm8    6 年前

    可能是因为项目源的类型是 ObservableCollection<string> ObservableCollection<ProperClassImplementation> (这是遗留的原因)。

    是的,没错。不能修改 string 因为它是不变的。你需要绑定到一个 一串 属性,这意味着您需要替换 可观察收集<字符串> .

    恐怕装订机不能代替机器 一串 可观察收集<字符串> 用一个新的 一串 如果这是你所希望的。