【问题标题】:WPF Get selected item from DataGrid and open a new WindowWPF 从 DataGrid 中获取所选项目并打开一个新窗口
【发布时间】:2015-02-13 07:24:58
【问题描述】:

我有一个 DataGrid,在 DataGrid 中列出了来自数据库的用户。 我想要这样,当我单击 DataGrid 中的成员时,打开一个新窗口,并向我显示成员数据(用户名、邮政编码……等等数据库中的内容)。 当我点击一行时如何打开一个新窗口?

已解决:

   private void dg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var windowToOpen = new UsersWindow();

        windowToOpen.Show();
    }

XAML:

<DataGrid  x:Name="NameGrid" MouseLeftButtonDown="dg_MouseLeftButtonDown"/>

【问题讨论】:

  • 很高兴听到这个消息。你的问题是什么?你试过什么?
  • 我的问题是:当我点击 DataGrid 行时,如何打开一个新窗口?

标签: wpf datagrid


【解决方案1】:

你是在使用 MVVM 还是在做代码隐藏?

在任何情况下,您都应该能够收听 selectedItemChanged 并按自己的方式处理

编辑

好的。从来没有自己试过这个.. DataGrid 有一个 SelectedItem 属性。这将为您提供选择中的第一行(如果您有多选)。

所以基本上你可以在你的代码隐藏中做:

private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
  var selectedUser = this.theNameOfYourGrid.SelectedItem;
  var windowToOpen = new Window();
  /*
   * Do what you want with your window
   * 
   * 
   */
  windowToOpen.Show();
}

在你的 XAML 中

<DataGrid SelectionChanged="Selector_OnSelectionChanged" x:Name="theNameOfYourGrid">

</DataGrid>

应该是这样的

【讨论】:

  • 好的...所以请发布您的代码作为修改后的问题。我去看看
  • 已解决:D private void dg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { var windowToOpen = new UserWindow(); windowToOpen.Show(); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-21
相关资源
最近更新 更多