I have a wpf MainWindow.xaml. The MainWindow includes a navigation page Type.xaml. And the navigation page has dataGrid and a popup Window AddType.xaml. Now I want the MainWindow to be refreshed after I close the popup Window. Below is my code:
MainWindow.xaml
- <Window x:Class="MyAccount.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:MyAccount"
- mc:Ignorable="d"
- Title="My personal budget" Height="700" Width="1200" WindowStartupLocation="CenterScreen">
- <Grid>
- <StackPanel>
- <Menu Name="menu1" >
- <MenuItem Header="Expenses" x:Name="typeExpense" Click="typeExpense_Click"/>
- </Menu>
- </StackPanel>
- <Frame Name="Main" NavigationUIVisibility="Hidden" Margin="0,35,0,0">
- </Frame>
- </Grid>
- </Window>
MainWindow CodeBehind:
- using System.Windows;
-
- namespace MyAccount
- {
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- private void typeExpense_Click(object sender, RoutedEventArgs e)
- {
- Type type = new Type();
- Main.NavigationService.Navigate(type);
- }
- }
- }
Type.xaml:
- <Page x:Class="MyAccount.Type"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:local="clr-namespace:MyAccount"
- mc:Ignorable="d"
- d:DesignHeight="650" d:DesignWidth="1150"
- Title="Type">
- <Grid>
- <Button x:Name="btnAdd" Content="Dobavit" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="50,45,0,0" Click="btnAddWindow_Click"/>
- <DataGrid x:Name="dataGrid" AutoGenerateColumns="False" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,100,10,10">
- <DataGrid.Columns>
- <DataGridTextColumn Header="Naimenovaniya" Binding="{Binding Path=typename}" Width="300"/>
- </DataGrid.Columns>
- </DataGrid>
- </Grid>
- </Page>
Type Code Behind:
- using System.Windows;
- using System.Windows.Controls;
- using System.Configuration;
- using System.Data.SqlClient;
- using System.Data;
-
- namespace MyAccount
- {
-
-
-
- public partial class Type : Page
- {
- string connection = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
- public Type()
- {
- InitializeComponent();
- DataGridBind();
- }
- protected void DataGridBind()
- {
- SqlConnection con = new SqlConnection(connection);
- con.Open();
- SqlCommand cmd = new SqlCommand("SELECT * from type", con);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- da.Fill(dt);
- dataGrid.ItemsSource = dt.DefaultView;
- }
-
- private void btnAddWindow_Click(object sender, RoutedEventArgs e)
- {
- AddType addtype = new AddType();
- addtype.Show();
- }
- }
- }
AddType.xaml:
- <Window x:Class="MyAccount.AddType"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:MyAccount"
- mc:Ignorable="d"
- Title="Dobavit kategorii" Height="200" Width="500">
- <Grid>
- <Label x:Name="label" Content="Naimenovaniya" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="35,0,0,0"/>
- <TextBox x:Name="txtType" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="364" Margin="45,26,0,0"/>
- <Button x:Name="btnOK" Content="OK" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="85,77,0,0" Click="btnOK_Click"/>
- <Button x:Name="btnCancel" Content="Otmena" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="219,77,0,0" Click="btnCancel_Click"/>
-
- </Grid>
- </Window>
AddType CodeBehind
- using System.Configuration;
- using System.Data.SqlClient;
- using System.Windows;
-
- namespace MyAccount
- {
- public partial class AddType : Window
- {
- string connection = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
- public AddType()
- {
- InitializeComponent();
- }
-
- private void btnOK_Click(object sender, RoutedEventArgs e)
- {
- SqlConnection con = new SqlConnection(connection);
- con.Open();
- SqlCommand cmd = new SqlCommand("INSERT INTO type (typename) VALUES ('"+txtType.Text+"')", con);
- cmd.ExecuteNonQuery();
- con.Close();
- Close();
- }
-
- private void btnCancel_Click(object sender, RoutedEventArgs e)
- {
- Close();
- }
-
- }
- }