Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将datagrid ItemsSource从页面加载到窗口_C#_Wpf_Xaml_Wpf Controls - Fatal编程技术网

C# 将datagrid ItemsSource从页面加载到窗口

C# 将datagrid ItemsSource从页面加载到窗口,c#,wpf,xaml,wpf-controls,C#,Wpf,Xaml,Wpf Controls,我正在开发一个使用NavigationWindow的应用程序,如下所示: // on datagrid row selection changed, it should load the ItemsSource in the Window1 datagrid. dg3 is the datagrid in Window1. private void dgDetails_SelectionChanged(object sender, SelectionChangedEventArgs e) {

我正在开发一个使用
NavigationWindow
的应用程序,如下所示:

// on datagrid row selection changed, it should load the ItemsSource in the Window1 datagrid. dg3 is the datagrid in Window1. 
private void dgDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    this.db = new testDB_Entities();
    string IDMapper = (dgDetails.SelectedItem as Details).Name;
    var Query1 = from a in this.db.Details
               orderby a.ID == IDMapper
               select a;

    dg3.DataContext = null;
    dg3.DataContext = Query1;
    dg3.Items.Refresh();
}
  • NavigationWindow
    作为
    Mainwindow

  • Page0.xaml
    Master Detail
    scenairo中有一个2
    DataGrid的
    (dgMaster和dgDetail)

  • Window1.xaml
    将在dgDetails的
    行双击的
    事件设置器上显示为
    ShowDialog()
    ,如下所示:

    // on datagrid row selection changed, it should load the ItemsSource in the Window1 datagrid. dg3 is the datagrid in Window1. 
    private void dgDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        this.db = new testDB_Entities();
        string IDMapper = (dgDetails.SelectedItem as Details).Name;
        var Query1 = from a in this.db.Details
                   orderby a.ID == IDMapper
                   select a;
    
        dg3.DataContext = null;
        dg3.DataContext = Query1;
        dg3.Items.Refresh();
    }
    

  • 代码隐藏

    public void Row_DoubleClick(object sender, RoutedEventArgs e)
    {
        Window1 my_Window = new Window1();
        my_Window.ShowDialog();
    }
    
    对于第2点,代码段如下所示:

    // on datagrid row selection changed, it should load the ItemsSource in the Window1 datagrid. dg3 is the datagrid in Window1. 
    private void dgDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        this.db = new testDB_Entities();
        string IDMapper = (dgDetails.SelectedItem as Details).Name;
        var Query1 = from a in this.db.Details
                   orderby a.ID == IDMapper
                   select a;
    
        dg3.DataContext = null;
        dg3.DataContext = Query1;
        dg3.Items.Refresh();
    }
    
    上述代码一起将
    窗口
    显示为
    对话框
    ,但
    数据网格
    为空。如何从
    页面0.xaml dgDetails\u SelectionChanged
    事件加载
    窗口1.xaml
    数据网格的
    项源

    我知道这些控件分别属于每个xaml文件,但是否有一种方法可以显示来自另一个xaml的控件数据上下文(无论页面/窗口如何)


    如果有人不明白这个问题。请让我知道。。我会尽量解释得更好

    如果我理解正确,问题是在窗口中加载gridview。 在这种情况下,一个选项是在窗口中创建公共集合(列表),并使用此公共列表设置dg3的项源

    public List<String> source {set;get}
    
    更改dgdetail选择后,将窗口中的列表指定为dgdetail中的集合

    private void dgDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
       {
       this.db = new testDB_Entities();
       string IDMapper = (dgDetails.SelectedItem as Details).Name;
       var Query1 = (from a in this.db.Details
                   orderby a.ID == IDMapper
                   select a).ToList();
    
    my_Window.source=Query1

       }
    

    您正在使用selection changed event,但您正在使用ShowDialog()打开window.xaml

    ShowDialog()将锁定页面,因此无法更改选择

    如果您正在使用ShowDialog()并只希望在对话框中显示,为什么不传递参数(我认为是:

    string IDMapper = (dgDetails.SelectedItem as Details).Name
    
    到窗口的构造函数

    public void Row_DoubleClick(object sender, RoutedEventArgs e)
    {
    
     string IDMapper = (dgDetails.SelectedItem as Details).Name;
     Window1 my_Window = new Window1(IDMapper );
     my_Window.ShowDialog();
    }
    
    然后在窗口构造函数或加载的事件中执行查询

    Window(string IDMapper)
    {
        var Query1 = (from a in this.db.Details
                   orderby a.ID == IDMapper
                   select a).ToList();
    }
    
    您需要从页面和窗口访问数据,您应该将EF移动到一个数据层中,这两个数据层都可以访问上下文,否则您只需将查询结果列表传递给窗口构造函数


    您还应该为您的Window.xaml选择一个不同的名称,以避免混淆,因为您与Window冲突,因此拥有Window1,

    如果投票有意义,否则请让我在这里了解有关问题的详细信息。感谢更新,但请您告诉我应该在哪里播放列表..在我的页面或窗口中?在t中双击事件时datagrid位于第0页,会显示一个对话框(这是已经使用datagrid设计的window1)。我必须在对话框上加载此datagrid。它只是窗口的一个属性。可能与公共类window1类似:Window{public List source{get;set}public window1(){InitilizeComponent();dg.ItemSource=source;}