双向绑定 ObservableCollection到 WPF 数据网格
我已经将不同的事情联系起来/阅读这个问题有一段时间了,但尚未找到答案。希望你们能帮忙。
我有一个字符串类型的可观察集合。我想将此集合绑定到数据网格并能够编辑/删除/添加到集合。这是我的 xaml:
<DataGrid ItemsSource="{Binding Movies.Titles}" CanUserDeleteRows="True" CanUserAddRows="True" Height="300">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=DataContext, RelativeSource={RelativeSource Self}}"/>
</DataGrid.Columns>
</DataGrid>
相同的 observablecollection 也绑定到列表框。我希望能够使用数据网格方法(上面)编辑集合并查看列表框中的更改/编辑。删除/添加工作正常,但是当我在网格单元格内编辑字符串并且它失去焦点时,该字符串会返回到原来的状态并且永远不会更新。
非常感谢您的任何帮助/建议。
I've been tying different things / reading up on this issue for awhile now and have not yet found an answer. Hopefully you guys can help.
I have an observablecollection of type string. I want to bind this collection to a datagrid and be able to edit/delete/add to the collection. Here is my xaml:
<DataGrid ItemsSource="{Binding Movies.Titles}" CanUserDeleteRows="True" CanUserAddRows="True" Height="300">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=DataContext, RelativeSource={RelativeSource Self}}"/>
</DataGrid.Columns>
</DataGrid>
The same observablecollection is also bound to a listbox. I want to be able to edit the collection using the datagrid method (above) and see the changes/edits in the listbox. The delete/add is working correctly, but when I edit a string inside a grid cell and it loses focus, the string goes back to what it originally was and never gets updated.
Thanks a lot for any help / suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哇,我昨天去做这件事,却被 DataGrid 困住了,它会为我的 ObservableCollection 添加新行。经过研究,我明白了原因。字符串且不可变。
我发现了这个问题,不幸的是它没有答案。所以我不能让这个问题没有答案。
以下是我找到的答案:
DataGrid 无法通过添加、编辑或删除字符串来更新字符串集合。
我找到了一种将字符串包装在 StringWrapper 对象中的解决方法。就是这里。
公共类StringWrapper
{
公共字符串文本{获取;放; }
。
我不喜欢这两个答案
最初的提问者 moncadad 看起来他想要一个单列 DataGrid。他可能只想在 ObservableCollection 中添加和删除字符串,而无需编写大量代码。编辑可能不太重要,因为可以通过删除并再次添加来完成。
我最终自己使用我制作的名为 StringListBox 的可重用用户控件来完成此操作。
字符串列表框支持添加和删除
基本上,这个想法是创建一个带有标签、列表框、文本框和添加按钮的 DataGrid 外观,因为这是一个控件,所以它必须与 ObservableObject 或 List 一起使用控制。
这给你添加和删除。我不提供编辑。
希望这对下一个人有帮助。
Wow, I went to do this yesterday and was stuck with a DataGrid that would add a new line for my ObservableCollection. After research, I realized why. Strings and immutable.
I found this question and unfortunately it didn't have an answer. So I can't leave this empty of an answer.
So here are the answers I found:
DataGrid cannot update a string collection by adding, editing, or removing strings.
I found a workaround to wrap the string in a StringWrapper object. Here it is.
public class StringWrapper
{
public string Text { get; set; }
}
I didn't like either answer.
The original question asker, moncadad, looks like he wants a one column DataGrid. He probably just wants to add and remove strings from an ObservableCollection without a lot of code. Edit probably isn't too important since that can be done by delete and add again.
I ended up doing this myself with a reusable usercontrol I made called StringListBox.
A ListBox for strings that supports add and delete
Basically the idea is to create the look of a DataGrid with a Label, a ListBox, a TextBox and an Add button and since this is a control, it has to work with ObservableObject or List in one control.
This give you Add and Delete. I doesn't provide edit.
Hope this helps the next guy.
实际上它是有效的,你应该在你的绑定中使用
。
我希望这有帮助!
Actually it works, you should just use
in your binding.
I hope this helps!