728x90
반응형
[버튼생성]
[MainWindow.xaml.cs]
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 WpfApp1
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Grid grid = new Grid(); //외관 테두리
this.Content = grid;
Button button = new Button();
button.Content = "호랑이";
button.HorizontalAlignment = HorizontalAlignment.Left;
//마진설정
button.Margin = new Thickness(150); //두께 150
button.VerticalAlignment = VerticalAlignment.Top;
button.Width = 75;
grid.Children.Add(button);
}
}
}
Background="#FFF4FF00" // 색상값을 설정할 때는 #으로 설정한다.
라이브 시각적 트리로 이동 : 디버깅 모드에서만 볼 수 있다.
[MainWindow.xaml.cs]
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 WpfApp1
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("1");
}
private void ListBoxItem_Selected(object sender, RoutedEventArgs e)
{
ListBoxItem listBoxItem = e.Source as ListBoxItem;
MessageBox.Show(ListBoxItem.ContentProperty.ToString());
}
}
}
[격자사용하는 논리]
[MainWindow.xaml]
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="486.8" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<Label Name="nameLabel"
Margin="2"
Grid.Row="0"
Grid.Column="0">_Name:</Label>
<TextBox
x:Name="nameText"
Grid.Row="0"
Grid.Column="1"
Margin="2"
Text="{Binding Name, Mode=OneWay}"/>
<Label Name="ageLabel"
Margin="2"
Grid.Row="1"
Grid.Column="0">_Age:</Label>
<TextBox
x:Name="ageText"
Grid.Row="1"
Grid.Column="1"
Margin="2"
Text="{Binding Age, Mode=OneWay}"/>
<StackPanel
Grid.Row="2"
Grid.ColumnSpan="2">
<Button Content="Click"
Click="f1"/>
</StackPanel>
</Grid>
</Window>
[MainWindow.xaml.cs]
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 WpfApp1
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void f1(object sebder, RoutedEventArgs e)
{
MessageBox.Show("1");
}
}
}
[Binding](1)
[MainWindow.xaml]
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="486.8" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<Label Name="nameLabel"
Margin="2"
Grid.Row="0"
Grid.Column="0">
_Name:</Label>
<TextBox
x:Name="nameText"
Grid.Row="0"
Grid.Column="1"
Margin="2"
Text="{Binding Name, Mode=TwoWay}"/>
<Label Name="ageLabel"
Margin="2"
Grid.Row="1"
Grid.Column="0">
_Age:</Label>
<TextBox
x:Name="ageText"
Grid.Row="1"
Grid.Column="1"
Margin="2"
Text="{Binding Age, Mode=TwoWay}"/>
<StackPanel
Grid.Row="2"
Grid.ColumnSpan="2">
<Button
Content="Click"
Click="f1"/>
</StackPanel>
<StackPanel
Grid.Row="2"
Grid.ColumnSpan="2">
<Button
Content="Click"
Click="f2"/>
</StackPanel>
</Grid>
</Window>
[MainWindow.xaml.cs]
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 WpfApp1
{
public class Person
{
public string nameValue;
public string Name
{
get { return nameValue; }
set { nameValue = value; }
}
private double ageValue;
public double Age
{
get { return ageValue; }
set
{
if(value != ageValue)
{
ageValue = value;
}
}
}
}
public partial class MainWindow : Window
{
Person person = new Person { nameValue = "호랑이", Age = 30 };
public MainWindow()
{
InitializeComponent();
this.DataContext = person;
}
private void f1(object sebder, RoutedEventArgs e)
{
string message = person.nameValue + " " + person.Age;
MessageBox.Show(message);
}
}
}
[StackPanel]
1 Background 2 Children 3 Height
4 ItemHeight 5 ItemWidth 6 LogicalChildren
7 LogicalOrientation 8 Margin
9 Name 10 Orientation
11 Parent 12 Resources 13 Style
14 Width
[MainWindow.xaml]
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="486.8" >
<Grid Background="#ff00ff00">
<StackPanel Orientation="Vertical" Background="#ff00ffff">
<!--Orientation="Horizontal"-->
<Button Content="호랑이1" Margin="10" Width="100" Height="30"/>
<Button Content="호랑이2" Margin="10" Width="100" Height="30"/>
<Button Content="호랑이3" Margin="10" Width="100" Height="30"/>
<TextBlock
x:Name="textBlock"
TextWrapping="Wrap"
Text="TextBlock"
Margin="5"
Width="100"
Height="30"
Background="#ffff00"/>
</StackPanel>
</Grid>
</Window>
[MainWindow.xaml.cs]
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 WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
[WrapPanel]
wrapanel: 줄바꿈
[MainWindow.xaml]
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="486.8" >
<Grid Background="#ff00ff00">
<WrapPanel Orientation="Horizontal" Background="#ff00ffff">
<Button Content="호랑이1" Margin="10" Width="100" Height="30"/>
<Button Content="호랑이2" Margin="10" Width="100" Height="30"/>
<Button Content="호랑이3" Margin="10" Width="100" Height="30"/>
<Button Content="호랑이4" Margin="10" Width="100" Height="30"/>
<Button Content="호랑이5" Margin="10" Width="100" Height="30"/>
<TextBlock
x:Name="textBlock"
TextWrapping="Wrap"
Text="TextBlock"
Margin="5"
Width="100"
Height="30"
Background="#ffff00"/>
</WrapPanel>
</Grid>
</Window>
[StackPanel]
[MainWindow.xaml]
indow x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="484.745" >
<Grid Background="#ff00ff00">
<StackPanel Orientation="Vertical" Background="#ff00ffff" Margin="0,0,0.4,0">
<TextBlock Text="Fist Name" Width="60" Height="20" Margin="5"/>
<TextBox Width="200" Height="20" Margin="5"/>
<TextBlock Text="Last Name" Width="60" Height="20" Margin="5"/>
<TextBox Width="200" Height="20" Margin="5"/>
<TextBlock Text="Age" Width="60" Height="20" Margin="5"/>
<TextBox Width="60" Height="20" Margin="5"/>
<TextBlock Text="Title" Width="60" Height="20" Margin="5"/>
<TextBox Width="200" Height="20" Margin="5"/>
</StackPanel>
</Grid>
</Window>
[DockPanel]
[MainWindow.xaml]
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="484.745" >
<Grid Background="#ff00ff00">
<DockPanel LastChildFill="True">
<Button Content="Top"
DockPanel.Dock="Top"
Click="Click_Me"/>
<Button Content="Bottom"
DockPanel.Dock="Bottom"
Click="Click_Me"/>
<Button Content="Left"
Click="Click_Me"/>
<Button Content="Right"
DockPanel.Dock="Right"
Click="Click_Me"/>
<Button Content="Center"
Click="Click_Me"/>
</DockPanel>
</Grid>
</Window>
[MainWindow.xaml.cs]
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 WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Click_Me(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
string str = btn.Content.ToString();
MessageBox.Show(str);
}
}
}
[Canvas]
[MainWindow.xaml]
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="484.745" >
<Grid Background="#ff00ff00">
<Canvas Width="580" Height="360">
<Ellipse
Canvas.Left="30"
Canvas.Top="30"
Fill="Gray"
Width="200"
Height="120"/>
<Ellipse
Canvas.Left="30"
Canvas.Bottom="30"
Fill="Gainsboro"
Width="100"
Height="100"/>
<Rectangle
Canvas.Left="350"
Canvas.Top="237"
Fill="LightBlue"
Width="50"
Height="50"/>
<Line
X1="5"
Y1="5"
X2="195"
Y2="95"
Stroke="blue"
StrokeThickness="5" />
<Ellipse
Canvas.Left="350"
Canvas.Top="110"
Stroke="Blue"
StrokeThickness="5"
Width="100"
Height="100"/>
<Label
Canvas.Left="80"
Canvas.Top="5"
Content="호랑이"/>
<Rectangle
Canvas.Left="140"
Canvas.Top="110"
Width="200"
Height="200"
Stroke="Black"
Fill="Green"
StrokeThickness="10"/>
</Canvas>
</Grid>
</Window>
[GridPanel]
[MainWindow.xaml]
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="484.745" >
<Grid x:Name="FormLayoutGrid"
Background="Green">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Grid.Column="0"
Text="Name"
Margin="10"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Width="100"/>
<TextBox Grid.Row="0"
Grid.Column="1"
Margin="10"/>
<TextBlock Grid.Row="1"
Grid.Column="0"
Text="ID"
Margin="10"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Width="100"/>
<TextBox Grid.Row="1"
Grid.Column="1"
Margin="10"/>
<TextBlock Grid.Row="2"
Grid.Column="0"
Text="Age"
Margin="10"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Width="100"/>
<TextBox Grid.Row="2"
Grid.Column="1"
Margin="10"/>
</Grid>
</Window>
[Grid]
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="484.745" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Background="#ff123456"
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="2">Button 1</Button>
<Button Background="#ffff5678"
Grid.Row="0"
Grid.Column="2">Button 2</Button>
<Button Background="#ffffff00"
Grid.Row="1"
Grid.Column="0">Button 3</Button>
<Button Background="#ff00ff78"
Grid.Row="1"
Grid.Column="1"
Grid.RowSpan="2"
Grid.ColumnSpan="2"
>Button 4</Button>
<Button Grid.Row="2"
Grid.Column="0">Button 5</Button>
</Grid>
</Window>
[grid]
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="484.745" >
<Grid Background="AntiqueWhite">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label
Content="Employee Info"
FontSize="15"
FontWeight="Bold"
Grid.Column="0"
Grid.Row="0"/>
<StackPanel
Grid.Column="0"
Grid.Row="1"
Orientation="Horizontal">
<Label
Content="Name"
VerticalAlignment="Center"
Width="70"/>
<TextBox
Name="txtName"
Text="Muhammad Ali"
VerticalAlignment="Center"
Width="200">
</TextBox>
</StackPanel>
<StackPanel
Grid.Column="0"
Grid.Row="2"
Orientation="Horizontal">
<Label
Content="ID"
VerticalAlignment="Center"
Width="70"/>
<TextBox Name="txtCity"
Text="421"
VerticalAlignment="Center"
Width="50">
</TextBox>
</StackPanel>
<StackPanel
Grid.Column="0"
Grid.Row="3"
Orientation="Horizontal">
<Label Content="Age"
VerticalAlignment="Center"
Width="70"/>
<TextBox Name="txtState"
Text="32"
VerticalAlignment="Center"
Width="50">
</TextBox>
</StackPanel>
<StackPanel
Grid.Column="0"
Grid.Row="4"
Orientation="Horizontal">
<Label
Content="Title"
VerticalAlignment="Center"
Width="70"/>
<TextBox
Name="txtCountry"
Text="Programmer"
VerticalAlignment="Center"
Width="200">
</TextBox>
</StackPanel>
</Grid>
</Window>
[바인딩 모드 개념]
- Binding 방식
- One Way 방식: Binding Source Property가 변경되면 Binding Target Property도 변경된다. 그러나 Binding Target ProPerty가 변경되도 Binding Source Property는 변하지 않는다.
- Two Way 방식: Binding Source Property가 변경되면 Binding Target Property는 변경된다. 그리고 Binding Target ProPerty가 변경되면 Source Property도 변경된다.
- OneWayToSource 방식: One way 방식과 반대 방식으로 동작
[Binding](2)
[MainWindow.xaml]
TwoWay
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="486.8" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox
x:Name="nameText" Width="100"
Grid.Row="0" Grid.Column="0"
Text="{Binding Name, Mode=TwoWay}"/>
<Button Grid.Row="1" Grid.Column="0"
Click="Button_Click1" Content="클릭1" />
<Button Grid.Row="2" Grid.Column="0"
Click="Button_Click2" Content="클릭2" />
</Grid>
</Window>
[MainWindow.xaml.cs]
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;
using System.ComponentModel;
using System.Drawing;
namespace WpfApp1
{
class Person : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public partial class MainWindow : Window
{
Person person = new Person { Name = "호랑이" };
public MainWindow()
{
InitializeComponent();
this.DataContext = person;
}
private void Button_Click1(object sender, RoutedEventArgs e)
{
MessageBox.Show(person.Name);
}
private void Button_Click2(object sender, RoutedEventArgs e)
{
person.Name = "독수리";
}
}
}
[Binding](3)
[MainWindow.xaml]
OneWay
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="486.8" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox
x:Name="nameText" Width="100"
Grid.Row="0" Grid.Column="0"
Text="{Binding Name, Mode=OneWay}"/>
<Button Grid.Row="1" Grid.Column="0"
Click="Button_Click1" Content="클릭1" />
<Button Grid.Row="2" Grid.Column="0"
Click="Button_Click2" Content="클릭2" />
</Grid>
</Window>
[MainWindow.xaml.cs]
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;
using System.ComponentModel;
using System.Drawing;
namespace WpfApp1
{
class Person : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public partial class MainWindow : Window
{
Person person = new Person { Name = "호랑이" };
public MainWindow()
{
InitializeComponent();
this.DataContext = person;
}
private void Button_Click1(object sender, RoutedEventArgs e)
{
MessageBox.Show(person.Name);
}
private void Button_Click2(object sender, RoutedEventArgs e)
{
person.Name = "독수리";
}
}
}
[Binding](4) 참조
[MainWindow.xaml]
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;
using System.ComponentModel;
using System.Drawing;
namespace WpfApp1
{
class Person : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set { name = value; OnPropertyChanged("Name"); }
}
private int age;
public int Age
{
get { return age; }
set { age = value; OnPropertyChanged("Age"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
//PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
// 윗 코드와 동일 코드
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public partial class MainWindow : Window
{
Person person = new Person { Name = "호랑이", Age = 20 };
public MainWindow()
{
InitializeComponent();
this.DataContext = person;
}
private void Button_Click1(object sender, RoutedEventArgs e)
{
MessageBox.Show(person.Name + " " + person.Age);
}
// View 객체(person)
// TwoWay : <-->
// OneWay : <---
// OneWayToSource --->
private void Button_Click2(object sender, RoutedEventArgs e)
{
person.Name = "독수리";
person.Age = 999;
}
}
}
[MainWindow.xaml.cs]
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="486.8" >
<StackPanel>
<TextBox
x:Name="nameText" Width="100"
Text="{Binding Name, Mode=TwoWay}"/>
<TextBox
x:Name="ageText" Width="100"
Text="{Binding Age, Mode=TwoWay}"/>
<Button Click="Button_Click1" Content="클릭1" />
<Button Click="Button_Click2" Content="클릭2" />
</StackPanel>
</Window>
[Reference]
728x90
반응형
'Education > Edu | .net' 카테고리의 다른 글
# 36.2) [WPF] 기본다지기4 (binding) (0) | 2021.03.08 |
---|---|
# 36.1) [WPF] 기본다지기3 (binding) (0) | 2021.03.08 |
# 34) [WPF] 기본다지기1 (0) | 2021.03.04 |
# 33) [C#] Winform 3 (VS2017에서 Oracle11g로 데이터 연동) (0) | 2021.03.03 |
# 32) [C#] Winform 2 (0) | 2021.03.02 |