C# 如何正确绑定ItemTemplate';s文本框文本到列表的字符串元素?

C# 如何正确绑定ItemTemplate';s文本框文本到列表的字符串元素?,c#,wpf,C#,Wpf,我有一个名为path的列表,它存储字符串元素。在MainWindow中,我创建了一个ItemControl来显示文本框,这些文本框应该绑定到路径的字符串元素。例如,如果路径由两个字符串元素组成,主窗口应该显示两个文本框,一个显示“Hello”,一个显示“World”,绑定应该是双向的。那么,我应该如何正确地完成装订工作呢 备注:我知道我应该使用ObservableCollection并将其绑定到ItemControl的ItemSource,但我不知道正确的方法 主窗口 <ItemsCont

我有一个名为
path
的列表,它存储字符串元素。在MainWindow中,我创建了一个ItemControl来显示文本框,这些文本框应该绑定到
路径的字符串元素。例如,如果路径由两个字符串元素组成,主窗口应该显示两个文本框,一个显示“Hello”,一个显示“World”,绑定应该是双向的。那么,我应该如何正确地完成装订工作呢

备注:我知道我应该使用ObservableCollection并将其绑定到ItemControl的ItemSource,但我不知道正确的方法

主窗口

<ItemsControl ItemsSource="{Binding PathsCollection}">>
        <ItemsControl.ItemsPanel>
              <ItemsPanelTemplate>
                     <StackPanel Orientation="Vertical"/>
               </ItemsPanelTemplate>
         </ItemsControl.ItemsPanel>
         <ItemsControl.ItemTemplate>
              <DataTemplate>
                   <TextBox Text="{Binding Path=?????, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
              </DataTemplate>
         </ItemsControl.ItemTemplate>
</ItemsControl>
>
DataContext

公共类SomeClass
{
私有列表路径;
公共列表路径
{
获取{返回_路径;}
设置{u路径=值;}
}
公共可见收集路径收集
{
得到
{//返回什么????
}
设置
{
//设置什么????
}
}
}

更新1

下面是我修改代码后的预期效果:

<ItemsControl ItemsSource="{Binding PathsCollection,  UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">>
        <ItemsControl.ItemsPanel>
              <ItemsPanelTemplate>
                     <StackPanel Orientation="Vertical"/>
               </ItemsPanelTemplate>
         </ItemsControl.ItemsPanel>
         <ItemsControl.ItemTemplate>
              <DataTemplate>
                   <TextBox Text="{Binding ., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
              </DataTemplate>
         </ItemsControl.ItemTemplate>
</ItemsControl>
>
公共类SomeClass
{
私有列表路径;
公共列表路径
{
获取{返回_路径;}
设置{u路径=值;}
}
公共可见收集路径收集
{
得到
{ 
返回新的ObservableCollection(路径);
}
设置
{
路径=value.ToList();
}
}
}

但是,它只在应用程序的第一次初始化时起作用。当我在
文本框
中添加、删除或修改某些文本时,它无法更改
\u路径
的任何字符串元素。为什么?

正如克莱门斯所说,您需要创建一个表示字符串的
类。例如,您可以将该类定义为:

public class PathItem : INotifyPropertyChanged
{
    private string path;
    public string Path
    {
        get { return path; }
        set
        {
            path = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
private ObservableCollection<PathItem> pathsCollection;
public ObservableCollection<PathItem> PathsCollection
{
    get { return pathsCollection ?? (pathsCollection = new ObservableCollection<PathItem>()); }
}
在C#类中,您可以像这样使用该类:

public class PathItem : INotifyPropertyChanged
{
    private string path;
    public string Path
    {
        get { return path; }
        set
        {
            path = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
private ObservableCollection<PathItem> pathsCollection;
public ObservableCollection<PathItem> PathsCollection
{
    get { return pathsCollection ?? (pathsCollection = new ObservableCollection<PathItem>()); }
}
您的xaml看起来像:

<ItemsControl ItemsSource="{Binding PathsCollection,  UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">>
        <ItemsControl.ItemsPanel>
              <ItemsPanelTemplate>
                     <StackPanel Orientation="Vertical"/>
               </ItemsPanelTemplate>
         </ItemsControl.ItemsPanel>
         <ItemsControl.ItemTemplate>
              <DataTemplate>
                   <TextBox Text="{Binding Path, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
              </DataTemplate>
         </ItemsControl.ItemTemplate>
</ItemsControl>
>

这不适用于编辑<代码>{Binding Path=.}
不会替换源集合中的字符串。您必须使用带有字符串属性的项集合,该属性还实现INotifyPropertyChanged。那么如何同时处理ObservableCollection和List?如何正确转换它们?@TomFong,如原始问题的答案所示:
observedcollection
@Clemens这不是我想要的。@TomFong你想要的
。对于字符串集合,没有办法做到这一点。