简体   繁体   中英

WPF different results, same code from different file

What I'm trying to do is update a GridView on a page. In my page behind code I have a method that returns a DataTable from a query from a database. I then want to set the DataContext of the DataGrid to the returned DataTable. If the GridView is on the main window of the application I can run the code and set the DataContext, but if I try to do the same from even the behind code of the page the GridView doesn't change. I've set breakpoints and I can verify that the DataTable does contain valid data. Why will the GridView work in the main window, but not in the page?

I'm not sure what code to include, but here's what I can think of to include.

Code that returns the DataTable is in the items.xaml.cs file.

public partial class EAHQ_Items : Page
{
  public static DataTable ReadAll(string keyword)
  {
    CRUD.sql = "SELECT id, name " +
               "FROM eahqItems " +
               "WHERE name LIKE @keyword1 OR @keyword2 ORDER BY id ASC";

    string strKeyword = string.Format("%{0}%", keyword);

    CRUD.cmd = new MySqlCommand(CRUD.sql, CRUD.con);
    CRUD.cmd.Parameters.Clear();
    CRUD.cmd.Parameters.AddWithValue("keyword1", strKeyword);
    CRUD.cmd.Parameters.AddWithValue("keyword2", keyword);

    DataTable dt = CRUD.PerformCRUD(CRUD.cmd);
    return dt;
  }
}

I have an items.xaml file with a corresponding items.xaml.cs file. In the main window there is a frame that displays the items.xaml file. The items.xaml file contains the following controls.

<Grid>
  <DataGrid x:Name="EAHQ_DG_Items" Margin="0,50,0,0" ItemsSource="{Binding}"/>
  <Label Content="EAHQ Items" HorizontalAlignment="Center" Margin="0,20,0,0" VerticalAlignment="Top"/>
</Grid>

The main windows have the following controls.

<DataGrid x:Name="dgItems" Grid.Column="2" Margin="10,120,10,0" Grid.Row="1" VerticalAlignment="Top" Height="100" ItemsSource="{Binding}"/>

The main windows behind code has the following line when the application loads. It's loading the DataContext of the control in the main window.

dtItems.DataContext = items.ReadAll("");

This code is what is in the behind code of the page.

public static void LoadData(string keyword)
{
  EAHQ_Items p = new EAHQ_Items();
  p.EAHQ_DG_Items.DataContext = ReadAll("");
}

As you can see, both lines run the same code, but the page DataGrid doesn't update. Sorry for the long post, but it's the best way I could think to ask.

Thank you!

As you can see, both lines run the same code

No, they certainly don't. You are creating a new instance of EAHQ_Items in LoadData and then set the DataContext of this one.

Your setup is a bit unclear but you should just be able to set the DataContext of the control in the code-behind of the page, just like you do in the window:

public partial class Items : Page
{
    public Items()
    {
        InitializeComponent();
        Loaded += OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        EAHQ_DG_Items.DataContext = ReadAll("");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM