Popup 컨트롤 - 컨트롤과 코드 기반으로 팝업 출력
Popup 컨트롤 - 팝업창을 띄울 수 있는 영역 제공
아래 그림과 같이 '솔루션 탐색기'에서 "이미지파일"인 "img0"파일을 붙여넣는다.
[MainPage.xaml]
<UserControl x:Class="RiaPopup.MainPage"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel>
<Button x:Name="btnAdd" Content="방명록 글쓰기(팝업창 열기)" />
<Button x:Name="btnCode" Content="코드기반으로 팝업창 열기" />
</StackPanel>
<Popup x:Name="myPopup" IsOpen="False" HorizontalOffset="20" VerticalOffset="20">
<Border BorderBrush="Red" BorderThickness="2" Width="320" Height="240">
<StackPanel>
<TextBlock Text="이름 : " />
<TextBlock Text="내용 : " />
<Button x:Name="btnClose" Content="팝업창 닫기" />
</StackPanel>
</Border>
</Popup>
</Grid>
</UserControl>
[MainPage.xaml.cs]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Imaging;
namespace RiaPopup
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
btnAdd.Click += new RoutedEventHandler(btnAdd_Click);
btnClose.Click += new RoutedEventHandler(btnClose_Click);
btnCode.Click += new RoutedEventHandler(btnCode_Click);
}
void btnCode_Click(object sender, RoutedEventArgs e)
{
Popup p = new Popup();
Image i = new Image();
i.Source = new BitmapImage(new Uri("img0.jpg", UriKind.Relative));
i.Width = 320;
i.Height = 240;
//i.MouseLeftButtonDown += new MouseButtonEventHandler(i_MouseLeftButtonDown);
i.MouseLeftButtonDown += delegate(object ss, MouseButtonEventArgs ee) { p.IsOpen = false; };
p.Child = i; // 이미지 등록
p.IsOpen = true; // 오픈
}
//void i_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
//{
// p.IsOpen = false;
//}
void btnClose_Click(object sender, RoutedEventArgs e)
{
this.myPopup.IsOpen = false; // 닫기
}
void btnAdd_Click(object sender, RoutedEventArgs e)
{
this.myPopup.IsOpen = true; // 열기
}
}
}