【问题标题】:Get ListView value获取 ListView 值
【发布时间】:2014-02-04 19:00:33
【问题描述】:

我正在尝试从列表视图框中获取数据但没有成功。

<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 中获取数据,但我尝试过的所有途径都只给了我列表中所有数据的字符串。我尝试使用:

DataRow selectedRow = lst_CallData.SelectedItem as DataRow;

但当前上下文中不存在数据行。

【问题讨论】:

  • 使用DataBinding
  • 我在程序的其余部分使用 dataBinding,但我不确定如何在此列表视图中使用它。
  • &lt;ListView ItemSource="{Binding SomeCollection}" SelectedItem="{Binding SomeItem}"/&gt; - 就是这样。
  • 您是否愿意写一个简短的答案,@HighCore,请^-^。此标签需要更多回答的问题...
  • 如果您使用 DataTable 对象作为 ItemsSource,那么它应该返回 DataRowView。您可以从 DataRowView 的 Row 属性中获取 DataRow。

标签: c# wpf listview selecteditem


【解决方案1】:

在编写 WPF 时,您必须花时间正确组织数据类型类。我的意思是,不要使用带有陈旧的DataRow 元素的可怕的旧DataTable ......相反,定义你自己的类,一个完全适合目的的类。重要的是,如果您有自己的类,那么您可以开始利用 INotifyPropertyChangedINotifyDataErrorInfo 等接口来帮助进行自动 UI 更新和数据验证。

如果你为你的数据创建了一个类,那么你可以在一个属性中拥有一个充满它们的ObservableCollection,比如说命名为SomeCollection。如果您添加了另一个名为 SomeItem 的类类型的属性,则可以将这些属性数据绑定到 @HighCore 正确显示的 ListView

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

然后要随时从代码或视图模型中访问所选项目,您只需访问SomeItem 属性即可。由于此属性属于您的自定义类的类型,因此您现在可以简单地按名称访问其任何属性。例如,如果你有相关的属性和需求,你可以这样做:

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

所以无论如何,那个是 HighCore 所逃避的,也是 MVVM 开发人员建议的方式,但也有一种更快的方式。如果您在ListView 中有数据绑定项,那么您可以直接从后面的代码中访问ListView.SelectedItem 属性(如果您已命名您的ListView

YourDataType selectedItem = (YourDataType)lst_CallData.SelectedItem;

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

【讨论】:

    【解决方案2】:

    以下是我尝试并能够获取数据行或整个表对象的方式:

    [XAML]

       <Grid>
        <Grid.DataContext>
            <localData:MyData />
        </Grid.DataContext>
        <ListView Name="lst_CallData" Width="500" ItemsSource="{Binding MyTable}" SelectionChanged="lst_CallData_SelectionChanged" 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>
    </Grid>
    

    [C#]

    private void lst_CallData_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView selectedRowView = this.lst_CallData.SelectedItem as DataRowView;
            if(selectedRowView != null)
            {
                DataRow selectedRow = selectedRowView.Row; // To get the partcular data row
                DataTable inputTable = selectedRowView.DataView.Table; //To get the entire table data
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多