如何在wpf中的文本框中显示listview所选行 [英] how to display listview selected row in textbox in wpf

查看:364
本文介绍了如何在wpf中的文本框中显示listview所选行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在listview中添加了几行。如何点击其中一行并将行显示到文本框。



  private   void  testListView_SelectionChanged( object  sender,SelectionChangedEventArgs e)
{
txtName.Text = testListView.SelectedItems [ 0 ]。ToString;
}







这个不行。



请帮忙。

解决方案

首先,我没有严格的证据证明你的方法 testListView_SelectionChanged 实际上被用作添加到列表视图的右侧实例的事件实例的调用列表的处理程序。你需要检查一下。它通过事件实例上的+ =运算符完成。如果你提出这样的问题,你应该总是展示它的作用。 (但是在WPF中,如果你使用设计器,这段代码是自动生成的,不是源代码的一部分;你可以在项目的子目录下找到它。)



因此,我总是更喜欢使用'+ ='运算符显式添加事件处理程序。 (顺便说一句,永远不要使用自动生成的名称,它们违反了Microsoft的命名约定,并且不是为永久使用而设计的;总是将这些名称重命名为语义上合理的名称。)



另外,您如何保证在处理时选择任何东西?检查元素[0]是否存在总是好的,通过检查计数

http://msdn.microsoft.com/en-us/library/system。 collections.icollection.count%28v = vs.110%29.aspx [ ^ ](这是 System.Collections.IList 通过 SelectedItems 属性的类型实现。



更好的是,使用 SelectedItem SelectedIndex

http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector。 selecteditem(v = vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us /library/system.windows.controls.primitives.selector.selectedindex(v=vs.110).aspx [ ^ ]。



在这种情况下,您仍然需要检查 SelectedIndex 是否为0或者 SelectedItem 是不是null。



至于 SelectedItems (复数!)这仅在您使用多选选项时有用。请参阅:

http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.selecteditems%28v=vs.110%29.aspx [ ^ ](参见注释:当SelectionMode不等于Single时,将使用此属性。如果选择模式为Single,则使用的正确属性为SelectedItem。),

http://msdn.microsoft.com/en -us / library / system.windows.controls.listbox.selectionmode%28v = vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows .controls.selectionmode%28v = vs.110%29.aspx [ ^ ]。



-SA

I added a few rows to my listview. How i can click on one of the row and display the row to textbox.

private void testListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txtName.Text = testListView.SelectedItems[0].ToString;
        }




This one doesn't work.

Please help.

解决方案

First I have no strict evidence that your method testListView_SelectionChanged was actually used as a handler added to the invocation list of the event instance of the right instance of the list view. You need to check it up. It is done via the "+=" operator on an event instance. If you ask such questions, you should always show where it is done. (But in WPF, if you use the designer, this code is auto-generated and is not a part of the source code; you can find it under the sub-directory of the project).

So, I always prefer to explicitly add the event handlers with the '+=' operator. (By the way, never use auto-generated names, they violate Microsoft naming conventions and not designed for permanent use; always rename those names to something semantically sensible.)

Also, how do you guarantee that anything is selected at the moment of handling? It would be always good to check up if the element [0] exist, by checking up Count:
http://msdn.microsoft.com/en-us/library/system.collections.icollection.count%28v=vs.110%29.aspx[^] (this is the property of System.Collections.IList implemented by the type of SelectedItems property).

Better yet, use SelectedItem or SelectedIndex:
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selecteditem(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectedindex(v=vs.110).aspx[^].

In this case, you still need to check up that SelectedIndex is 0 or more or SelectedItem is not null.

As to SelectedItems (plural!) this is only useful if you use multi-select option. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.selecteditems%28v=vs.110%29.aspx[^] (see the comment: "This property is meant to be used when SelectionMode does not equal Single. If the selection mode is Single the correct property to use is SelectedItem."),
http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.selectionmode%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.selectionmode%28v=vs.110%29.aspx[^].

—SA


这篇关于如何在wpf中的文本框中显示listview所选行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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