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
"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">
-
-
- >
- x:Name="typeExpense" Click="typeExpense_Click"/>
- "Main" NavigationUIVisibility="Hidden" Margin="0,35,0,0">
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);
- }
- }
- }
"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">
-
- Content="Dobavit" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="50,45,0,0" Click="btnAddWindow_Click"/>
-
"dataGrid" AutoGenerateColumns="False" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,100,10,10"> -
-
"Naimenovaniya" Binding="{Binding Path=typename}" Width="300"/>
- using System.Windows;
- using System.Windows.Controls;
- using System.Configuration;
- using System.Data.SqlClient;
- using System.Data;
- namespace MyAccount
- {
- ///
- /// Interaction logic for Type.xaml
- ///
- 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();
- }
- }
- }
"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">
-
- Content="Naimenovaniya" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="35,0,0,0"/>
-
"txtType" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="364" Margin="45,26,0,0"/> - Content="OK" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="85,77,0,0" Click="btnOK_Click"/>
- Content="Otmena" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="219,77,0,0" Click="btnCancel_Click"/>
- 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();
- }
- }
- }
Amit GuptaPosted Aug 23, 2017, 2:59 AM