获取ListView控件值 [英] Get ListView value

查看:86
本文介绍了获取ListView控件值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个列表视图框获取数据,并已成功。

I am trying to get data from a listview box and have been unsuccessful.

<ListView Name="lst_CallData" Width="950" Height="500" Grid.Row="1" Grid.ColumnSpan="2" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Col0}" Width="150" />
      <GridViewColumn Header="Num" DisplayMemberBinding="{Binding Col1}" Width="200" />
      <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Col2}" Width="200" />
    </GridView>
  </ListView.View>
 </ListView>

我试图获取数据col2的,但我已经尝试了所有的途径都只能给我列表中的所有数据的字符串。我试图用

I am trying to get the data out of Col2 but all avenues I have tried have only given me a string of all the data in the list. I tried to use:

DataRow selectedRow = lst_CallData.SelectedItem as DataRow;

但数据行在目前情况下不存在。

but datarow does not exists in the current context.

推荐答案

在编写WPF,你必须采取适当整理数据类型类的时间。这样,我的意思是不使用旧可怕数据表其陈旧的的DataRow 元素......取而代之的是,自己的类,一个正好适合目的。重要的是,如果你有你自己的类,然后就可以开始利用像<一个接口href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx\"相对=nofollow> INotifyPropertyChanged的 和<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifydataerrorinfo%28v=vs.110%29.aspx\"相对=nofollow> INotifyDataErrorInfo 以帮助自动更新的用户界面和数据验证。

When writing WPF, you have to take the time to organise your data type classes properly. By this, I mean don't use a horrible old DataTable with its antiquated DataRow elements... instead, define your own class, One that suits the purpose exactly. Importantly, if you have your own class, then you can start to take advantage of interfaces like INotifyPropertyChanged and INotifyDataErrorInfo to help with automatic UI updates and data validation.

如果你创建了一个类为您的数据,那么你可以有一个的ObservableCollection 充斥其中的一个属性,可以说名为 SomeCollection 。如果您添加类的命名类型 SomeItem 的另一个属性,然后你可以将数据绑定这些属性到的ListView 作为@HighCore正确显示你:

If you created a class for your data, then you could have an ObservableCollection full of them in a property, lets say named SomeCollection. If you added another property of the type of your class named SomeItem, you could then data bind these properties to the ListView as @HighCore correctly showed you:

<ListView ItemSource="{Binding SomeCollection}" SelectedItem="{Binding SomeItem}" />

然后在任何时候从后面或视图模型code访问选定的项目,你可以访问 SomeItem 属性。由于这个属性是自​​定义类的类型,那么你现在可以简单地通过名称来访问其任何属性。例如,如果你有相关属性和需要,你可以做这样的事情:

Then to access the selected item at any time from the code behind or view model, you can just access the SomeItem property. As this property is of the type of your custom class, then you can now simply access any of its properties by name. For example, if you had the relevant properties and the need, you could do something like this:

public void SaveTextFile()
{
    File.WriteAllText(SomeItem.FilePath, SomeItem.Text);
}

所以无论如何,的是HighCore被规避,以和MVVM开发商会建议的方式,但有一个更快的方式了。如果你在的ListView 数据绑定物品,那么你可以直接从$访问 ListView.SelectedItem 属性C $ç后面如果有一个名为您的的ListView

So anyway, that is what HighCore was eluding to and the way that MVVM developers would suggest, but there is a much quicker way too. If you have data bound items in the ListView, then you can just access the ListView.SelectedItem property directly from the code behind if you have named your ListView:

YourDataType selectedItem = (YourDataType)lst_CallData.SelectedItem;

...其中 YourDataType 是绑定到 ListView的数据集合中对象的类型

这篇关于获取ListView控件值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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