将ObservableCollection绑定到模板化ListBox

Age*_*ire 1 data-binding wpf templates listbox observablecollection

我有这个系列

ObservableCollection<string> Urls { get; set; }
Run Code Online (Sandbox Code Playgroud)

在我的数据上下文类中.我在列表框中绑定了它:

<ListBox ItemsSource="{Binding Urls}">

    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding .}" />
        </DataTemplate>
    </ListBox.ItemTemplate>

</ListBox>
Run Code Online (Sandbox Code Playgroud)

数据显示在列表框中,两个相应的 - 未显示 - 这里的按钮带有命令Add和Delete也可以工作,但是,更改TextBox不会影响集合的内容.

我试过Mode=TwoWay绑定,但我认为它已经打开了.我已经尝试了一些其他选项,如Validate = OnPropertyChange,但是,仍然没有任何更新.

如何使实际TextBox模板中的内部ListBox实际更新Urlsdatacontext类的属性?

H.B*_*.B. 6

你不能修改字符串; 使用包含一个字符串属性的包装类,然后绑定TextBox到所述属性.这样,属性中的字符串可以用编辑的字符串替换.