본문 바로가기
프로그래밍/UWP | WPF

[WPF] Window 이용한 Popup Dialog Alert Box 생성

by 남생 namsaeng 2022. 8. 19.
반응형

기본 WPF에서 제공하는 Message Box는 예쁘지 않아서 커스터마이징 한 Window를 Message Box 대신하여 사용한다.

 

 

<MainPage.xaml>

<Page x:Class="WpfTestProject2.MainPage"
      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:WpfTestProject2"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="MainPage">

    <Grid>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="0.02*"/>
                <RowDefinition Height="0.1*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Border Grid.Row="2" Background="Beige">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="0.3*"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Border Grid.Row="0">
                        <Border.Background>
                            <LinearGradientBrush EndPoint="0.4,1" StartPoint="0.3,0">
                                <GradientStop Color="Blue"/>
                                <GradientStop Color="SkyBlue" Offset="0.5"/>
                            </LinearGradientBrush>
                        </Border.Background>
                        <StackPanel VerticalAlignment="Center">
                            <TextBlock Text="Welcome WPF Pop-up Dialog!" FontSize="35" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        </StackPanel>
                    </Border>
                    <Border Grid.Row="1">   
                        <Button x:Name="Popup_Button" Background="OrangeRed" VerticalAlignment="Center" HorizontalAlignment="Center" BorderBrush="LightYellow" BorderThickness="7" Height="100" Width="400" Click="Popup_Button_Click">
                            <TextBlock Foreground="Aqua" FontSize="25" FontWeight="Bold">Press this for Pop-up Dialog!</TextBlock>
                        </Button>
                    </Border>
                </Grid>
            </Border>
        </Grid>
    </Grid>
</Page>

 

 

 

<MainPage.xaml.cs>

namespace WpfTestProject2
{
    public partial class MainPage : Page
    {

        public MainPage()
        {
            InitializeComponent();
        }

        private void Popup_Button_Click(object sender, RoutedEventArgs e)
        {
            PopupWindow popupWindow = new PopupWindow();
            popupWindow.ShowDialog();
        }
    }
}

 

 

<PopupWindow.xaml>

<Window x:Class="WpfTestProject2.PopupWindow"
        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:WpfTestProject2"
        mc:Ignorable="d"
        WindowStyle="None"
        WindowStartupLocation="CenterScreen"
        Height="600" Width="600"
        Background="Beige">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.2*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Border Grid.Row="0">
            <Border.Background>
                <LinearGradientBrush EndPoint="0.4,1" StartPoint="0.3,0">
                    <GradientStop Color="Gray"/>
                    <GradientStop Color="LightGray" Offset="0.5"/>
                </LinearGradientBrush>
            </Border.Background>
            <StackPanel VerticalAlignment="Center">
                <TextBlock Text="Save a lovely beluga!" FontSize="35" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </StackPanel>
        </Border>
        <Border Grid.Row="1">
            <Grid>
                <Grid.Background>
                    <ImageBrush ImageSource="Assets/Beluga.JPG" Stretch="Fill"/>
                </Grid.Background>
                <StackPanel VerticalAlignment="Bottom" Margin="0,0,0,30">
                    <Button x:Name="Popup_Button" Background="Aquamarine" VerticalAlignment="Center" HorizontalAlignment="Center" BorderBrush="LightYellow" BorderThickness="7" Height="100" Width="500" Click="Popup_Button_Click">
                        <TextBlock Foreground="Red" FontSize="25" FontWeight="Bold">Escape the beluga from the aquarium!</TextBlock>
                    </Button>
                </StackPanel>
            </Grid>
        </Border>
    </Grid>
</Window>

 

 

 

<PopupWindow.xaml.cs>

namespace WpfTestProject2
{
    public partial class PopupWindow : Window
    {
        public PopupWindow()
        {
            InitializeComponent();
        }

        private void Popup_Button_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}

 

 

 

WPF Window 이용하여 Pop-up Dialog Alert Box 만들기
WPF Window 이용하여 Pop-up Dialog Alert Box 만들기

 

 

 

 

 

반응형

댓글