0

I am developing an application which uses NavigationWindow as follows:

  1. NavigationWindow as Mainwindow.

  2. Page0.xaml which has a 2 DataGrid's (dgMaster and dgDetail) in Master Detail scenairo.

  3. Window1.xaml which will be displayed as ShowDialog() on dgDetails's Row_DoubleClick's event setter as follows:


Code behind

public void Row_DoubleClick(object sender, RoutedEventArgs e)
{
    Window1 my_Window = new Window1();
    my_Window.ShowDialog();
}

For point number 2, the code snippet is as follows:

// 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();
}

The above codes together displays the Window as a DialogBox, but the DataGrid is empty. How to load the ItemsSource of the DataGrid in Window1.xaml from the Page0.xaml dgDetails_SelectionChanged event?

I understand that these controls belong separately to each xaml files, but is there a way to display a controls datacontext from another xaml ( regardless a page /window).

if anybody dont understand the question. please let me know.. i will try to explain it better.

4

2 回答 2

1

您正在使用选择更改事件,但您正在使用 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 构造函数或 Loaded 事件中执行查询

Window(string IDMapper)
{
    var Query1 = (from a in this.db.Details
               orderby a.ID == IDMapper
               select a).ToList();
}

您需要从 Page 和 Window 访问数据,您应该将 EF 移动到都可以访问 Context 的数据层中,如果您只是将 Query 结果列表传递给 Window 构造函数

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

于 2013-04-24T19:39:59.277 回答
1

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

public List<String> source {set;get}

在构造函数中

Window()
{
    \\initialize
    dg3.ItemSources=source;
}

当您更改 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=查询1;

   }
于 2013-04-24T15:22:20.033 回答