본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

[ WPF & C# (C Sharp)] - 새 창 띄우기 및 전역변수 활용

728x90
반응형

【 WPF & C# (C Sharp) 】 - 새 창 띄우기 및 전역변수 활용



@ MainWindow.xaml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<Window x:Class="newWindow.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:newWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="212.849" Width="188.359">
    <Grid>
        <Button x:Name="btn1" Content="new Window" HorizontalAlignment="Left" Height="90" Margin="31,67,0,0" VerticalAlignment="Top" Width="120" Click="btn1_Click"/>
        <TextBox x:Name="txtBx1" HorizontalAlignment="Left" Height="23" Margin="31,39,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
 
    </Grid>
</Window>
 
cs



@ MainWindow.xaml.cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace newWindow
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Properties["aaa"= txtBx1.Text;
            Window Window1 = new Window1();
            Window1.Show();
        }
    }
}
 
cs



@ Window1.xaml


1
2
3
4
5
6
7
8
9
10
11
12
13
<Window x:Class="newWindow.Window1"
        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:newWindow"
        mc:Ignorable="d"
        Title="Window1" Height="117.557" Width="189.313" WindowStyle="ToolWindow" WindowStartupLocation="CenterScreen">
    <Grid>
        <Label x:Name="lbl1" Content="Label" HorizontalAlignment="Left" Margin="27,23,0,0" VerticalAlignment="Top" Width="120" Background="#FFE0E0E0"/>
    </Grid>
</Window>
 
cs



Window1.xaml.cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
 
namespace newWindow
{
    /// <summary>
    /// Window1.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            if (Application.Current.Properties["aaa"!= null)   // 값이 없으면 에러가 난다.
            {
                lbl1.Content = Application.Current.Properties["aaa"].ToString();
            }
        }
    }
}
cs



 @ 새 창 띄우기 및 전역변수 활용

새 창을 띄울때마다 변수 값을 가져오도록 되어 있다.

실시간으로 변경 되었으면하는데 아직 잘 모르겠다.

수시로 읽어들여야 되나... ㅠㅠ

전역 변수를 사용했고,

전역 변수값이 없을 때 오류가 나기 때문에 null 체크를 해놨다.


@ 만약 새 창이 떠 있는 상태에서 메인 창이 클릭이 안되도록 하려면 ShowDialog 로 띄우면 된다.


1
2
3
4
5
6
7
 
        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Properties["aaa"= txtBx1.Text;
            Window Window1 = new Window1();
            Window1.ShowDialog();
        }
cs




http://insurang.tistory.com


728x90
반응형