本文介绍了双向绑定ObservableCollection< string>到WPF DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个问题上解决了不同的事情/阅读了一段时间,还没有找到答案。希望你们可以帮忙。



我有一个可观察的类型字符串的集合。我想绑定这个集合到一个datagrid,并能够编辑/删除/添加到集合。这是我的xaml:

 < DataGrid ItemsSource ={Binding Movies.Titles}CanUserDeleteRows =TrueCanUserAddRows = TrueHeight =300> 
< DataGrid.Columns>
< DataGridTextColumn Binding ={Binding Path = DataContext,RelativeSource = {RelativeSource Self}}/>
< /DataGrid.Columns>
< / DataGrid>

同样的observablecollection也绑定到一个列表框。我想使用datagrid方法(上图)编辑该集合,并查看列表框中的更改/编辑。删除/添加工作正常,但是当我在网格单元格中编辑字符串并失去焦点时,字符串将返回到原来的位置,并且不会被更新。



非常感谢任何帮助/建议。

解决方案

哇,昨天我去做这个事情,被卡住了一个DataGrid这将为我的ObservableCollection添加一个新行。经过研究,我意识到为什么。字符串和不变。



我发现这个问题,不幸的是没有答案。所以我不能留下这个空白的答案。



所以这里是我找到的答案:


  1. DataGrid无法通过添加,编辑或删除字符串来更新字符串集。


  2. 我发现了一个解决方法来包装StringWrapper对象中的字符串。这里是。



    public class StringWrapper
    {
    public string Text {get;组;
    }


我不喜欢任何答案。



原来的问题提问者moncadad看起来像是要一列DataGrid。他可能只是想添加和删除一个ObservableCollection中的字符串,而不需要很多代码。编辑可能不是太重要,因为可以通过删除和添加再次完成。



最后我自己做了一个可重用的用户控件,我称之为StringListBox。 p>



基本上,想法是创建一个DataGrid的外观Label,一个ListBox,一个TextBox和一个Add按钮,因为这是一个控件,它必须在一个控件中使用ObservableObject或List。



这给你添加和删除。我不提供编辑。



希望这有助于下一个人。


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.

解决方案

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:

  1. DataGrid cannot update a string collection by adding, editing, or removing strings.

  2. 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.

这篇关于双向绑定ObservableCollection&lt; string&gt;到WPF DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 02:44