可编辑WPF列表框 [英] Editable WPF ListBox

查看:322
本文介绍了可编辑WPF列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ObservableCollection绑定到WPF中的ListBox。我想要列表框可编辑,编辑更改将保存到集合。由于WPF不提供可编辑的列表框,我已经尝试通过更改ListBox.ItemTemplate创建自己的。

 < ListBox。 ItemTemplate> 
< DataTemplate>
< TextBox Name =EditableTextText ={TemplateBinding Content}/>
< / DataTemplate>
< /ListBox.ItemTemplate>

更改ItemTemplate可以给我可编辑的框,但对文本框的任何更改都不会保存到ObservableCollection。有没有办法使用双向绑定的可编辑ListBox?

解决方案

你不能这样做。

为了达到这样的把戏,你需要把你的项目作为持有者类,揭示你可以绑定文本框的属性。



要理解它,想象下面的伪序列调用:

  class ListBox 
{
绑定(项目)
{
foreach(项目中的var项目)
{
DataTemplate模板= LoadTemplateForItem(item.GetType()); //这是你的模板加载
Template.Bind(item); //这是您的模板绑定的地方
}
}
}

您的模板(与列表框的DataTemplate)被加载,并且项目(我认为是你的情况下的一个字符串)被传入。
在这一点上,它只知道字符串,不能影响任何东西向上。双向绑定不能影响收集,因为模板不知道在哪个上下文中被使用,所以它不能回到原始集合并修改其内容。
对于这个问题,这对于TextBox是一回事。如果没有给出一个管理员和一个财产名称,它就无法存储变更。
这与将字符串传递给函数调用基本相同。该函数不能更改传入的字符串(忽略通过引用参数传递的技巧)。



要恢复您的情况,您需要构建一个暴露包含需要编辑的值的属性的对象:

  public class MyDataItem 
{
string Data {get; set;}
}

然后,您可以将ListBox绑定到这些项目的集合,修改您的数据表:

 < ListBox.ItemTemplate> 
< DataTemplate>
< TextBox Name =EditableTextText ={Binding Data,Mode = TwoWay}/>
< / DataTemplate>
< /ListBox.ItemTemplate>


I have a ObservableCollection that's bound to a ListBox in WPF. I want the ListBox to be editable, and for the editing changes to be saved to the collection. Since WPF doesnt provide an editable listbox, I've tried creating my own by changing the ListBox.ItemTemplate.

<ListBox.ItemTemplate>
    <DataTemplate>                      
        <TextBox Name="EditableText" Text="{TemplateBinding Content}"/>
    </DataTemplate>
</ListBox.ItemTemplate>

Changing the ItemTemplate gives me editable boxes, but any changes to the textboxes dont get saved to the ObservableCollection. Is there a way to have an editable ListBox with two way binding?

解决方案

You cannot do it this way.

To achieve that kind of trick, you would need your items to be "holder classes" that expose a property you can bind your textbox to.

To understand it, imagine the following pseudo sequence of calls:

class ListBox
{
  Bind(Items)
  {
    foreach(var item in Items)
    {
      DataTemplate Template = LoadTemplateForItem(item.GetType()); // this is where your template get loaded
      Template.Bind(item); //this is where your template gets bound
    }
  }
}

Your template (the DataTemplate with the listbox) is loaded and the item (which I assume is a string in your case) gets passed in. At this point, it only knows the string, and cannot influence anything upwards. A two-way binding cannot influence the collection because the template does not know in which context it is being used, so it cannot reach back to the original collection and modify its contents. For that matter, this is the same thing for the TextBox. If it is not given a conainer and a property name, it has nowhere to "store back" the changes. This basically the same as passing a string into a function call. The function cannot change which string was passed in (ignoring tricks such as by-reference argument passing).

To get back to your case, you need to build a collection of objects which expose a property containing the value that needs to be edited:

public class MyDataItem
{
  string Data { get; set;}
}

Then you can bind your ListBox to a collection of those items and modifiy your datatemplate:

<ListBox.ItemTemplate>
    <DataTemplate>                                              
            <TextBox Name="EditableText" Text="{Binding Data, Mode=TwoWay}"/>
    </DataTemplate>
</ListBox.ItemTemplate>

这篇关于可编辑WPF列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆