■ Binding 엘리먼트의 Source/Path/UpdateSourceTrigger 속성을 사용해 단순 바인딩을 설정하는 방법을 보여준다.
▶ Person.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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
using System.ComponentModel; namespace TestProject { /// <summary> /// 사람 /// </summary> public class Person : INotifyPropertyChanged { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 속성 변경시 - PropertyChanged /// <summary> /// 속성 변경시 /// </summary> public event PropertyChangedEventHandler PropertyChanged; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 이름 /// </summary> private string name; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이름 - PersonName /// <summary> /// 이름 /// </summary> public string PersonName { get { return this.name; } set { this.name = value; FirePropertyChangedEvent("PersonName"); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Person() /// <summary> /// 생성자 /// </summary> public Person() { } #endregion #region 생성자 - Person(name) /// <summary> /// 생성자 /// </summary> /// <param name="name">이름</param> public Person(string name) { this.name = name; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 속성 변경시 이벤트 발생시키기 - FirePropertyChangedEvent(name) /// <summary> /// 속성 변경시 이벤트 발생시키기 /// </summary> /// <param name="name">이름</param> protected void FirePropertyChangedEvent(string name) { PropertyChangedEventHandler handler = PropertyChanged; handler?.Invoke(this, new PropertyChangedEventArgs(name)); } #endregion } } |
▶ MainWindow.xaml
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <local:Person x:Key="PersonKey" PersonName="Joe" /> <Style TargetType="{x:Type Label}"> <Setter Property="DockPanel.Dock" Value="Top" /> </Style> <Style TargetType="{x:Type TextBox}"> <Setter Property="DockPanel.Dock" Value="Top" /> <Setter Property="Width" Value="100" /> <Setter Property="Height" Value="25" /> <Setter Property="VerticalContentAlignment" Value="Center" /> </Style> <Style TargetType="{x:Type TextBlock}"> <Setter Property="DockPanel.Dock" Value="Top" /> <Setter Property="Width" Value="100" /> <Setter Property="Height" Value="25" /> </Style> </Window.Resources> <Border Margin="10" CornerRadius="5" BorderThickness="5" BorderBrush="RoyalBlue" Padding="10"> <DockPanel Width="300" Height="110"> <Label>이름을 입력해 주시기 바랍니다 :</Label> <TextBox> <TextBox.Text> <Binding Source="{StaticResource PersonKey}" Path="PersonName" UpdateSourceTrigger="PropertyChanged" /> </TextBox.Text> </TextBox> <Label Margin="0 10 0 0">입력한 이름은 아래와 같습니다 :</Label> <TextBlock Margin="5 0 0 0" Text="{Binding Source={StaticResource PersonKey}, Path=PersonName}" /> </DockPanel> </Border> </Window> |