<Window x:Class="HelloWPF1.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:HelloWPF1"
mc:Ignorable="d"
Title="헬로우 WPF" Height="450" Width="450">
<Grid>
<Label x:Name="lbl" Content="레이블" Margin="184,83,167,314"/>
<Button x:Name="btn" Content="버튼" HorizontalAlignment="Center" Margin="0,229,0,0" VerticalAlignment="Top" RenderTransformOrigin="5.229,2.246"/>
</Grid>
</Window>
Label과 Button에 별칭을 지정해주고 아래에서 불러올 수 있다.
using System.Text;
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 HelloWPF1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
btn.Content = "버튼입니다.";
lbl.Content = "레이블입니다.";
}
}
}
btn.Content를 비하인드 코드에서 수정하고 적용할 수 있다.
<Window x:Class="WpfApp2.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:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel Orientation="Vertical">
<Label HorizontalContentAlignment="Center">콘텐트센터</Label>
<Label HorizontalContentAlignment="Left">콘텐트레프트</Label>
<Label HorizontalContentAlignment="Right">콘텐트라이트</Label>
<Label HorizontalContentAlignment="Stretch">콘텐트스트레치</Label>
<Label HorizontalAlignment="Center">얼라이먼트센터</Label>
<Label HorizontalAlignment="Left">얼라이먼트레프트</Label>
<Label HorizontalAlignment="Right">얼라이먼트라이트</Label>
<Label HorizontalAlignment="Stretch">얼라이먼트스트레치</Label>
<Label>
<Image Width="50" Source="https://www.cyso.co.kr/data/item/1625295618/thumb-64yA7ZGc1_500x500.jpg"></Image>
</Label>
<Label>
<StackPanel Orientation="Vertical">
<Image Width="50" Source="https://www.cyso.co.kr/data/item/1625295618/thumb-64yA7ZGc1_500x500.jpg"></Image>
<AccessText Text="원하는 문자열"/>
</StackPanel>
</Label>
<Label Content="Hello"
FontSize="30"
FontWeight="Bold"
Background="Salmon"
Foreground="White"
></Label>
<Label Content="_Lbl" Target="{Binding ElementName=tb}"></Label>
<TextBox x:Name="tb"/>
</StackPanel>
</Grid>
</Window>
<Label Content="Hello"
FontSize="30"
FontWeight="Bold"
Background="Salmon"
Foreground="White"
></Label>
위처럼 스타일을 지정할 수 있다.
<Label Content="_Lbl" Target="{Binding ElementName=tb}"></Label>
<TextBox x:Name="tb"/>

<Window x:Class="WpfTextBlock.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:WpfTextBlock"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="300">
<Grid>
<StackPanel>
<TextBlock>텍스트블록</TextBlock>
<TextBlock Text="안녕하세요"/>
<TextBlock> <Bold>Bold텍스트블록</Bold>
<LineBreak/>
<Italic>Italic입니다.</Italic>
<Hyperlink RequestNavigate="Hyperlink_RequestNavigate" NavigateUri="https://google.com" Cursor="Hand">구글 바로가기</Hyperlink>
</TextBlock>
<TextBlock
Background="Salmon"
Foreground="White"
>
<Span Foreground="Black">애국가 시작합니다,.</Span>애국가 동해물과 백두산이 마르로 닳도록 하느님이 보우하사 우리나라만세 무궁화 삼천리 화려강산 대한사람 대한으로 길이 보전하세 남산 위에 저 소나무 철갑을 두른 듯 바람 서리 불변함은 우리 기상일세 무궁화 삼천리 화려강산 대한사람 대한으로 길이 보전하세
</TextBlock>
<TextBlock
TextTrimming="CharacterEllipsis"
>
<Span Foreground="Black">애국가 시작합니다,.</Span>애국가 동해물과 백두산이 마르로 닳도록 하느님이 보우하사 우리나라만세 무궁화 삼천리 화려강산 대한사람 대한으로 길이 보전하세 남산 위에 저 소나무 철갑을 두른 듯 바람 서리 불변함은 우리 기상일세 무궁화 삼천리 화려강산 대한사람 대한으로 길이 보전하세
</TextBlock>
<TextBlock
TextTrimming="None"
>
<Span Foreground="Black">애국가 시작합니다,.</Span>애국가 동해물과 백두산이 마르로 닳도록 하느님이 보우하사 우리나라만세 무궁화 삼천리 화려강산 대한사람 대한으로 길이 보전하세 남산 위에 저 소나무 철갑을 두른 듯 바람 서리 불변함은 우리 기상일세 무궁화 삼천리 화려강산 대한사람 대한으로 길이 보전하세
</TextBlock>
<TextBlock
TextTrimming="WordEllipsis"
>
<Span Foreground="Black">애국가 시작합니다,.</Span>애국가 동해물과 백두산이 마르로 닳도록 하느님이 보우하사 우리나라만세 무궁화 삼천리 화려강산 대한사람 대한으로 길이 보전하세 남산 위에 저 소나무 철갑을 두른 듯 바람 서리 불변함은 우리 기상일세 무궁화 삼천리 화려강산 대한사람 대한으로 길이 보전하세
</TextBlock>
<TextBlock
TextWrapping="Wrap"
>
<Span Foreground="Black">애국가 시작합니다,.</Span>애국가 동해물과 백두산이 마르로 닳도록 하느님이 보우하사 우리나라만세 무궁화 삼천리 화려강산 대한사람 대한으로 길이 보전하세 남산 위에 저 소나무 철갑을 두른 듯 바람 서리 불변함은 우리 기상일세 무궁화 삼천리 화려강산 대한사람 대한으로 길이 보전하세
</TextBlock>
<TextBlock
TextWrapping="WrapWithOverflow"
>
<Span Foreground="Black">애국가 시작합니다,.</Span>애국가 동해물과 백두산이 마르로 닳도록 하느님이 보우하사 우리나라만세 무궁화 삼천리 화려강산 대한사람 <Underline>대한으로</Underline> 대한으로 길이 보전하세 남산 위에 저 소나무 철갑을 두른 듯 바람 서리 불변함은 우리 기상일세 무궁화 삼천리 화려강산 대한사람 대한으로 길이 보전하세
</TextBlock>
<TextBox x:Name="logTextBox" VerticalScrollBarVisibility="Auto" IsReadOnly="True"/>
</StackPanel>
</Grid>
</Window>

using System.Diagnostics;
using System.Text;
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 WpfTextBlock
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
logTextBox.AppendText(e.Uri.AbsoluteUri + "\n");
logTextBox.ScrollToEnd();
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri) {UseShellExecute = true });
}
}
- logTextBox.AppendText(e.Uri.AbsoluteUri + "\n");
- logTextBox.ScrollToEnd();
위는 내가 RequestNavigateEventArgs로 받은 e를 까보기 위해서 만들어놓은 디버깅 텍스트박스이다.
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri) {UseShellExecute = true });
위 코드로 하이퍼링크로 이동하는 것을 허용해준다.
<Window x:Class="WpfButton.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:WpfButton"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<Button>버튼</Button>
<Button Content="버튼"/>
<Button
x:Name="btn"
ToolTip="클릭해보세요"
Click="btn_Click"
MouseDoubleClick="btn_MouseDoubleClick"
MouseLeave="btn_MouseLeave"
MouseEnter="btn_MouseEnter"
Content="버튼" Width="100">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="20"></Setter>
</Style>
</Button.Resources>
</Button>
<TextBlock Text="텍스트블럭" x:Name="txtb"/>
</StackPanel>
</Grid>
</Window>
using System.Text;
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 WpfButton
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btn_Click(object sender, RoutedEventArgs e)
{
txtb.Text = "버튼을 클릭했습니다.";
}
private void btn_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
txtb.Background = Brushes.Salmon;
txtb.Foreground = Brushes.White;
}
private void btn_MouseLeave(object sender, MouseEventArgs e)
{
btn.Foreground = Brushes.Black;
}
private void btn_MouseEnter(object sender, MouseEventArgs e)
{
btn.Foreground = Brushes.Red;
}
}
}
각 이벤트가 실행될 때마다 디자인코드에서 받은 Name을 참조해 이벤트에 해당하는 동작을 수행하는 코드를 작성했다.
<Window x:Class="WpfTextBox.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:WpfTextBox"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<TextBox
x:Name ="txt"
IsReadOnly="true">텍스트박스</TextBox>
<TextBox
FontSize="20"
Background="Salmon"
Foreground="White"
Margin="10"
TextWrapping="Wrap"
AcceptsReturn="True"
Text="텍스트박스"
SelectionChanged="TextBox_SelectionChanged"
/>
<TextBlock>텍스트블럭</TextBlock>
</StackPanel>
</Grid>
</Window>
using System.Text;
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 WpfTextBox
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
{
TextBox textbox = sender as TextBox; //이벤트가 발생한 object에서 별칭을 사용하지 않았기 때문에 TextBox로 형변환
if(textbox != null)
{
txt.Text = textbox.Text;
}
}
}
}
<Window x:Class="WpfAppPassword.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:WpfAppPassword"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<PasswordBox BorderThickness="3"
PasswordChar="*"
MaxLength="3"
x:Name="pwd"
/>
<Button Click="Button_Click"></Button>
</StackPanel>
</Grid>
</Window>
pwd에 제대로 들어왔는지 보기 위해
Button Click 이벤트로 비하인드 코드에 내용을 보내어 확인해보자
using System.Text;
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 WpfAppPassword
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(pwd.Password);
}
}
}

위처럼 메시지 박스가 클릭시 입력한 값으로 잘 뜨는 것을 볼 수 있다.
<Window x:Class="WpfApp1Image.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:WpfApp1Image"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<Image
x:Name="img"
MouseEnter="img_MouseEnter"
Width="400"
Source="https://velog.velcdn.com/images/jckim22/post/6ae6fd72-6905-4aa5-84af-91daaba4ae9d/image.png"></Image>
<Button>
<ContentControl>
<StackPanel>
<Image
x:Name="img2"
MouseEnter="img_MouseEnter"
Width="400"
Source="https://velog.velcdn.com/images/jckim22/post/12eca916-3d46-4c75-85bf-9169ef313963/image.png"></Image>
<TextBlock HorizontalAlignment="Center">버튼입니다.</TextBlock>
</StackPanel>
</ContentControl>
</Button>
</StackPanel>
</Grid>
</Window>
using System.Text;
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 WpfApp1Image
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void img_MouseEnter(object sender, MouseEventArgs e)
{
img.Source = new BitmapImage(new Uri("https://velog.velcdn.com/images/jckim22/post/56253d13-73ca-472e-b8e3-0b93ebbcd164/image.png"));
}
}
}
<Window x:Class="체크박스.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:체크박스"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel HorizontalAlignment="Center">
<Label>과일</Label>
<CheckBox>딸기</CheckBox>
<CheckBox>사과</CheckBox>
<CheckBox IsChecked="true">수박</CheckBox>
<CheckBox>배</CheckBox>
<CheckBox
x:Name="chkApple"
Checked="chkApple_Checked"
Unchecked="chkApple_Unchecked"
VerticalContentAlignment="Center"
>
<ContentControl>
<StackPanel Orientation="Horizontal">
<Image Width="50" Source="https://velog.velcdn.com/images/jckim22/post/6ae6fd72-6905-4aa5-84af-91daaba4ae9d/image.png"></Image>
<TextBlock VerticalAlignment="Center" x:Name="tb">청사과</TextBlock>
</StackPanel>
</ContentControl>
</CheckBox>
</StackPanel>
</Grid>
</Window>
using System.Text;
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 체크박스
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void chkApple_Checked(object sender, RoutedEventArgs e)
{
tb.Text = "청사과 선택했습니다.";
}
private void chkApple_Unchecked(object sender, RoutedEventArgs e)
{
tb.Text = "청사과 선택취소.";
}
}
}

<Window x:Class="라디오.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:라디오"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel HorizontalAlignment="Center">
<Label>좋아하는 사람은?</Label>
<RadioButton x:Name ="rb" IsChecked="true" GroupName="first">홍길동</RadioButton>
<RadioButton GroupName="first">타요</RadioButton>
<RadioButton GroupName="first">임꺽정</RadioButton>
<RadioButton GroupName="first">장영실</RadioButton>
<Label>좋아하는 과일은?</Label>
<RadioButton GroupName="fruit">사과</RadioButton>
<RadioButton GroupName="fruit">배</RadioButton>
<RadioButton GroupName="fruit">오렌지</RadioButton>
<RadioButton
Checked="RadioButton_Checked"
Unchecked="RadioButton_Unchecked"
GroupName="fruit"
VerticalContentAlignment="Center">
<WrapPanel>
<Image Width="30" Source="https://velog.velcdn.com/images/jckim22/post/8b7bed83-5780-4d40-8614-701148a57b64/image.png"></Image>
<Label>청사과</Label>
</WrapPanel>
</RadioButton>
<GroupBox BorderThickness="3" Header="안녕하십니까">
<StackPanel>
<RadioButton>1</RadioButton>
<RadioButton>2</RadioButton>
<RadioButton>3</RadioButton>
<RadioButton>4</RadioButton>
</StackPanel>
</GroupBox>
<GroupBox BorderThickness="3">
<GroupBox.Header>
<WrapPanel>
<Image Width="30" Source="https://velog.velcdn.com/images/jckim22/post/8b7bed83-5780-4d40-8614-701148a57b64/image.png"></Image>
<Label>안녕하세요</Label>
</WrapPanel>
</GroupBox.Header>
<StackPanel>
<RadioButton>1</RadioButton>
<RadioButton>2</RadioButton>
<RadioButton>3</RadioButton>
<RadioButton>4</RadioButton>
</StackPanel>
</GroupBox>
</StackPanel>
</Grid>
</Window>
using System.Text;
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 라디오
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
rb.IsChecked = true;
}
private void RadioButton_Unchecked(object sender, RoutedEventArgs e)
{
rb.IsChecked = false;
}
}
}
<Window x:Class="캘린더.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:캘린더"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<StackPanel Orientation="Horizontal">
<Calendar DisplayMode="Decade"></Calendar>
<Calendar DisplayMode="Year"></Calendar>
<Calendar DisplayMode="Month"
x:Name="cal"
SelectedDatesChanged="cal_SelectedDatesChanged"
>
<Calendar.BlackoutDates>
<CalendarDateRange Start="07.01.2024"
End="07.05.2024"
/>
</Calendar.BlackoutDates>
</Calendar>
</StackPanel>
<TextBlock x:Name="tb">텍스트블럭</TextBlock>
<TextBlock
Text="{Binding ElementName=cal, Path=SelectedDate
,StringFormat='yyyy-MM-dd tt hh:mm:ss', ConverterCulture='ko-KR'
}"
></TextBlock>
</StackPanel>
</Grid>
</Window>
using System.Text;
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 캘린더
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void cal_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
{
tb.Text = cal.SelectedDate.ToString();
}
}
}
<Window x:Class="데이트피커.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:데이트피커"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<DatePicker/>
<DatePicker
SelectedDate="2024-06-30"
SelectedDateFormat="Long"
x:Name="dp"
SelectedDateChanged="dp_SelectedDateChanged"
/>
<TextBlock x:Name="tb">텍스트블럭</TextBlock>
<TextBlock Text="{Binding ElementName=dp, Path=Text}"></TextBlock>
</StackPanel>
</Grid>
</Window>
using System.Text;
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 데이트피커
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void dp_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
if(tb != null)
{
tb.Text = dp.Text;
}
}
}
}
<Window x:Class="슬라이더.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:슬라이더"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel VerticalAlignment="Center" Background="Salmon">
<TextBlock
FontSize="{Binding ElementName=sld, Path=Value}"
x:Name="tb" Text="텍스트" />
<Slider
x:Name="sld"
ValueChanged="sld_ValueChanged"
TickPlacement="TopLeft"
IsSnapToTickEnabled="True"
Minimum="10"
Maximum="100"
TickFrequency="10"
/>
</StackPanel>
</Grid>
</Window>
using System.Text;
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 슬라이더
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void sld_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
tb.Text = sld.Value.ToString();
}
}
}


<Window x:Class="익스팬더.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:익스팬더"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<Expander Header="확장" Expanded="Expander_Expanded">
<StackPanel>
<TextBlock>텍스트 블럭</TextBlock>
<Button>버튼</Button>
</StackPanel>
</Expander>
<Expander>
<Expander.Header>
<WrapPanel>
<TextBlock>확장</TextBlock>
</WrapPanel>
</Expander.Header>
<WrapPanel>
<TextBlock>텍스트 블럭</TextBlock>
<Button>확장</Button>
</WrapPanel>
</Expander>
</StackPanel>
</Grid>
</Window>
namespace 익스팬더
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Expander_Expanded(object sender, RoutedEventArgs e)
{
MessageBox.Show("관리자 권한입니다.");
}
}
}
<Window x:Class="스택패널.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:스택패널"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<Button>버튼</Button>
<Button>버튼</Button>
<Button>버튼</Button>
<Button>버튼</Button>
<StackPanel
HorizontalAlignment="Center"
Orientation="Horizontal">
<Button>버튼</Button>
<Button>버튼</Button>
<Button>버튼</Button>
<Button>버튼</Button>
<Button>버튼</Button>
</StackPanel>
<Button>버튼</Button>
<Button>버튼</Button>
</StackPanel>
</Grid>
</Window>
<DockPanel LastChildFill="True">
<Button DockPanel.Dock="Top">상단</Button> <--Top이 Left를 덮음-->
<Button DockPanel.Dock="Left">왼쪽</Button>
<Window x:Class="그리드.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:그리드"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.ColumnSpan="2">버튼1</Button>
<Button Grid.Column="2">버튼3</Button>
<Button Grid.Row="1" Grid.RowSpan="2" Grid.Column="0">버튼1-1</Button>
<Button Grid.Row="1" Grid.Column="1">버튼2-1</Button>
<Button Grid.Row="1" Grid.Column="2">버튼3-1</Button>
<Button Grid.Row="2" Grid.Column="1">버튼2-2</Button>
<Button Grid.Row="2" Grid.Column="2">버튼3-2</Button>
</Grid>
</Window>

<Window x:Class="프로그레스바.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:프로그레스바"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<ProgressBar Height="20" Value="50"
Minimum="0" Maximum="100"
IsIndeterminate="True"
></ProgressBar>
<Separator></Separator>
<Button Click="Button_Click">버튼</Button>
<ProgressBar Name="pb" Height="20" Maximum="100"/>
<TextBlock
Text="{Binding ElementName=pb, Path=Value, StringFormat={}{0:0}%}"
HorizontalAlignment="Center"
></TextBlock>
</StackPanel>
</Grid>
</Window>
namespace 프로그레스바
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
pb.Value += 10;
}
}
}
<Window x:Class="스테이터스바.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:스테이터스바"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<DockPanel LastChildFill="False">
<Slider
DockPanel.Dock="Top" Name="sl" Maximum="100"
ValueChanged="sl_ValueChanged"
></Slider>
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem>
준비
</StatusBarItem>
<Separator></Separator>
<StatusBarItem x:Name="sb">
로딩중..
</StatusBarItem>
<StatusBarItem>
<ProgressBar x:Name="pb" Height="10" Width="150" Value="0"></ProgressBar>
</StatusBarItem>
</StatusBar>
</DockPanel>
</Window>
using System.Security.Cryptography;
using System.Text;
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 스테이터스바
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void sl_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (sl.Value == 100)
{
sb.Content = "완료";
}
else
{
pb.Value = sl.Value;
sb.Content = "로딩중";
}
}
}
}
<Window x:Class="툴바.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:툴바"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<DockPanel>
<ToolBarTray DockPanel.Dock="Top">
<ToolBar>
<Button>
<Image Width="20" Source="https://cdn-icons-png.flaticon.com/128/16772/16772839.png"></Image>
</Button>
<TextBlock VerticalAlignment="Center">폰트사이즈</TextBlock>
<ComboBox x:Name="cb" SelectionChanged="cb_SelectionChanged">
<ComboBoxItem IsSelected="True">10</ComboBoxItem>
<ComboBoxItem>10</ComboBoxItem>
<ComboBoxItem>12</ComboBoxItem>
<ComboBoxItem>14</ComboBoxItem>
<ComboBoxItem>16</ComboBoxItem>
<ComboBoxItem>18</ComboBoxItem>
<ComboBoxItem>20</ComboBoxItem>
<ComboBoxItem>22</ComboBoxItem>
<ComboBoxItem>24</ComboBoxItem>
</ComboBox>
</ToolBar>
</ToolBarTray>
<StackPanel>
<TextBox x:Name="tb"></TextBox>
<TextBox x:Name="tb2" FontSize="{Binding ElementName=cb, Path=Text}"></TextBox>
</StackPanel>
</DockPanel>
</Window>
ComboBox의 Text를 바인딩해서 아래 TextBox의 크기를 정한다.
using System.Text;
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 툴바
{
///
private void cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (tb != null)
{
MessageBox.Show(cb.Text);
tb.FontSize = Convert.ToDouble(cb.Text);
}
}
}
}
- 이건 바인딩 대신 이벤트 발생 시 비하인드 코드에서 따로 처리한 것이다.
<Window x:Class="메뉴.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:메뉴"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="파일">
<MenuItem Header="파일">
<MenuItem Header="새로운 문서">
<MenuItem.Icon>
<Image Source="https://cdn-icons-png.flaticon.com/128/16772/16772855.png"></Image>
</MenuItem.Icon>
</MenuItem>
</MenuItem>
<MenuItem Header="열기"></MenuItem>
<MenuItem Header="닫기"></MenuItem>
</MenuItem>
<MenuItem Header="_Move" Click="MenuItem_Click"></MenuItem>
</Menu>
<ContentControl x:Name="ContentArea"></ContentControl>
</DockPanel>
</Window>
MenuItem으로 메뉴를 만든다.
ContentControl은 메뉴를 제외한 부분을 차지한다.
메뉴의 Move 메뉴 클릭시 이벤트를 발생시킨다.
namespace 메뉴
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
ContentArea.Content = new UserControl1();
}
}
}
<Window x:Class="컨텍스트메뉴.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:컨텍스트메뉴"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<TextBlock Text="마우스 우클릭" FontSize="20" Name="tb">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="진하게" Click="MenuItem_Click">
<MenuItem.Icon>
<Image Source="https://cdn-icons-png.flaticon.com/128/16772/16772855.png"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="기울기" IsCheckable="True"
Checked="MenuItem_Checked"
Unchecked="MenuItem_Unchecked">
<MenuItem.Icon>
<Image Source="https://cdn-icons-png.flaticon.com/128/16772/16772855.png"></Image>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
<Button Content="버튼 우클릭">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="진하게" Click="MenuItem_Click">
<MenuItem.Icon>
<Image Source="https://cdn-icons-png.flaticon.com/128/16772/16772855.png"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="기울기" IsCheckable="True"
Checked="MenuItem_Checked"
Unchecked="MenuItem_Unchecked">
<MenuItem.Icon>
<Image Source="https://cdn-icons-png.flaticon.com/128/16772/16772855.png"></Image>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</Button.ContextMenu>
</Button>
</StackPanel>
</Window>
namespace 컨텍스트메뉴
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
tb.FontWeight = FontWeights.Bold;
}
private void MenuItem_Checked(object sender, RoutedEventArgs e)
{
tb.FontStyle = FontStyles.Italic;
}
private void MenuItem_Unchecked(object sender, RoutedEventArgs e)
{
tb.FontStyle = FontStyles.Normal;
}
}
}
<Window x:Class="데이터바인딩.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:데이터바인딩"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<TextBox x:Name="tbox"></TextBox>
<TextBlock Text="{Binding ElementName=tbox, Path=Text}"></TextBlock>
<Label>소스</Label>
<TextBox Name="tbox1" Text="홍길동"></TextBox>
<Label>타겟</Label>
<TextBox Text="{Binding ElementName=tbox1, Path=Text, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<Button x:Name="btn1" Background="Salmon">버튼1</Button>
<Button Background="{Binding ElementName=btn1, Path=Background}">버튼2</Button>
</StackPanel>
</Window>
<Window x:Class="데이터바인딩2.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:데이터바인딩2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<Label Content="{Binding 이름}"></Label>
<Label Content="{Binding 나이}"></Label>
<WrapPanel Name="wp">
<Label Content="{Binding 이름}"></Label>
<Label Content="{Binding 나이}"></Label>
</WrapPanel>
<StackPanel Name="stp">
<Label Content="{Binding 이름}"></Label>
<Label Content="{Binding 나이}"></Label>
</StackPanel>
</StackPanel>
</Window>
namespace 데이터바인딩2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public string 이름 { get; set; }
public int 나이 { get; set; }
public MainWindow()
{
InitializeComponent();
이름 = "타요";
나이 = 10;
DataContext = this;
Person person = new Person() { 이름 = "홍길동", 나이 = 100 };
wp.DataContext = person;
Person person2 = new Person() { 이름 = "임꺽정", 나이 = 90 };
stp.DataContext = person2;
}
}
}

<Window x:Class="데이터바인딩3.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:데이터바인딩3"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<ListBox Grid.Column="0" Name="lb">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding 이름}"/>
<Label Grid.Column="1" Content="{Binding 나이}"/>
<ProgressBar Width="100" Grid.Column="2" Value="{Binding 별점}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListView Grid.Column="1" Name="lv">
<ListView.View>
<GridView>
<GridViewColumn Header="이름" DisplayMemberBinding="{Binding 이름}"></GridViewColumn>
<GridViewColumn Header="나이" DisplayMemberBinding="{Binding 나이}"></GridViewColumn>
<GridViewColumn Header="별점">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ProgressBar Width="50" Height="20" Value="{Binding 별점}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
namespace 데이터바인딩3
{
public class Person
{
public string 이름 { get; set; }
public int 나이 { get; set; }
public double 별점 { get; set; }
}
public partial class MainWindow : Window
{
List<Person> people = new List<Person>();
public MainWindow()
{
InitializeComponent();
people.Add(new Person() {이름 = "홍길동", 나이 = 45, 별점 = 74.2 });
people.Add(new Person() {이름 = "임꺽정", 나이 = 22, 별점 = 99.2 });
people.Add(new Person() {이름 = "장비", 나이 = 57, 별점 = 34.2 });
people.Add(new Person() {이름 = "유비", 나이 = 12, 별점 = 67.2 });
people.Add(new Person() {이름 = "관우", 나이 = 32, 별점 = 45.2 });
lb.ItemsSource = people;
lv.ItemsSource = people;
}
}
}

ListBox -> ItemTeplate -> DataTemplate -> Grid
위 경로에 바인딩한 리스트 데이터소스들을 넣으면 모든 요소가 반환이 된다.

ListView는 ListBox에서 파생된 컨트롤러다.
ListView