µ¥ÀÌÅÍ ¹ÙÀεù 6
Æ®¸®°Å¸¦ ÅëÇØ XAML¿¡¼ ±âº»ÀûÀÎ UI Á¦¾î°¡ °¡´ÉÇÏ´Ù. Play ¹öÆ°À» Ŭ¸¯Çϸé Stop ¹öÆ°ÀÌ È°¼ºÈ µÇ°í Play ¹öÆ°ÀÌ ºñÈ°¼ºÈ µÇ°í Button.StyleÀ» ÄÁÆ®·Ñ´ç Çϳª¾¿ ¹èÄ¡ÇÏ°í ÀÖ´Ù. <Grid>
<Grid.RowDefinitions> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <StackPanel Orientation="Horizontal"> <Button Content="Play" Height="23" Margin="5" Click="Execute_Click"> <Button.Style> <Style TargetType="Button"> <Setter Property="IsEnabled" Value="True"/> <Style.Triggers> <DataTrigger Binding="{Binding IsRunning}" Value="True"> <Setter Property="IsEnabled" Value="False"/> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button> <Button Content="Stop" Height="23" Margin="5" Click="Stop_Click"> <Button.Style> <Style TargetType="Button"> <Setter Property="IsEnabled" Value="False"/> <Style.Triggers> <DataTrigger Binding="{Binding IsRunning}" Value="True"> <Setter Property="IsEnabled" Value="True"/> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button> </StackPanel> </Grid> DataTrigger´Â Binding ¼Ó¼º °ªÀÌ Value ¿Í °°À¸¸é ³»ºÎÀÇ Setter¸¦ ½ÇÇàÇÑ´Ù.
Play ¹öÆ°Àº IsRunningÀÌ TrueÀ϶§ ¹öÆ°ÀÇ IsEnabled´Â FlalseÀÌ´Ù.
Stop ¹öÆ°Àº IsRunningÀÌ TrueÀ϶§ ¹öÆ°ÀÇ IsEnabled´Â TrueÀÌ´Ù. public partial class MainWindow : Window, INotifyPropertyChanged
{ private bool isRunning = false; public bool IsRunning { get { return isRunning; } set { isRunning = value; OnPropertyChanged("IsRunning"); } } public MainWindow() { InitializeComponent(); this.DataContext = this; } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string name) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } private void Execute_Click(object sender, RoutedEventArgs e) { IsRunning = true; } private void Stop_Click(object sender, RoutedEventArgs e) { IsRunning = false; } } Play ¹öÆ°À» Ŭ¸¯Çϸé Execute_Click( ) ¸Þ½îµå°¡ ½ÇÇàµÈ´Ù. ´Ù¿î·Îµå : BindingDemo_trigger.zip
ÄÞº¸¹Ú½º¿¡¼ »ö»ó ¼±ÅÃÇϱâÄÞº¸¹Ú½º¿¡¼ Ä®¶ó¸¦ ¼±ÅÃÇÏ¸é ¿·ÀÇ ÅؽºÆ®¿¡ Ç¥½ÃÇÑ´Ù. ÄÞº¸¹Ú½º¿¡¼ Ä®¶ó¸¦ ¼±ÅÃÇÏ¸é ¹ÙÀεù µÈ "Color"¿¡ ÀÇÇؼ TextBlockÀº ¹®ÀÚ¿À» "Color" »ö»óÀ¸·Î ¹Ù²Û´Ù. <Window.Resources>
<x:Array x:Key="ColorItems" Type="Color"> <Color>Black</Color> <Color>Red</Color> <Color>Blue</Color> <Color>Yellow</Color> <Color>Green</Color> </x:Array> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <StackPanel Orientation="Horizontal"> <ComboBox ItemsSource="{StaticResource ColorItems}" SelectedValue="{Binding Color, UpdateSourceTrigger=PropertyChanged}" Width="90" Height="23" Margin="5"> <ComboBox.ItemTemplate> <DataTemplate> <Border Width="80" Height="20" Margin="5,0"> <Border.Background> <SolidColorBrush Color="{Binding}"/> </Border.Background> </Border> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> <TextBlock Text="Text" Height="23" Margin="5"> <TextBlock.Foreground> <SolidColorBrush Color="{Binding Color}"/> </TextBlock.Foreground> </TextBlock> </StackPanel> </Grid>
using System.ComponentModel;
namespace BindingDemo { /// <summary> /// MainWindow.xaml¿¡ ´ëÇÑ »óÈ£ ÀÛ¿ë ³í¸® /// </summary> public partial class MainWindow : Window, INotifyPropertyChanged { private Color color = Colors.Black; public Color Color { get { return color; } set { color = value; OnPropertyChanged("Color"); } } public MainWindow() { InitializeComponent(); this.DataContext = this; } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string name) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } } }
´Ù¿î·Îµå : BindingDemo_ComboBox_Color.zip
DataGrid¿¡¼ Ä®¶ó ¼±ÅÃDataGrid¿¡¼ Ä®¶ó¸¦ ¼±ÅÃÇÏ¸é ¹®ÀÚ¿À» ÇØ´ç Ä®¶ó·Î ¾÷µ¥ÀÌÆ® ÇÑ´Ù. <Window x:Class="BindingDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:BindingDemo" Title="BindingDemo" Height="224" Width="289" DataContext="{Binding}"> <Window.Resources> <x:Array x:Key="ColorItems" Type="Color"> <Color>Black</Color> <Color>Red</Color> <Color>Blue</Color> <Color>Yellow</Color> <Color>Green</Color> </x:Array> <x:Array x:Key="DataGridItems" Type="local:TestClass"> <local:TestClass Color="Black" Text="AAA"/> <local:TestClass Color="Black" Text="BBB"/> <local:TestClass Color="Black" Text="CCC"/> </x:Array> </Window.Resources> <Grid> <DataGrid ItemsSource="{StaticResource DataGridItems}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridComboBoxColumn Header="»ö" ItemsSource="{StaticResource ColorItems}" SelectedValueBinding="{Binding Color, UpdateSourceTrigger=PropertyChanged}"> <DataGridComboBoxColumn.ElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemTemplate"> <Setter.Value> <DataTemplate> <Border Width="80" Height="20" Margin="5,0"> <Border.Background> <SolidColorBrush Color="{Binding}"/> </Border.Background> </Border> </DataTemplate> </Setter.Value> </Setter> </Style> </DataGridComboBoxColumn.ElementStyle> <DataGridComboBoxColumn.EditingElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemTemplate"> <Setter.Value> <DataTemplate> <Border Width="80" Height="20" Margin="5,0"> <Border.Background> <SolidColorBrush Color="{Binding}"/> </Border.Background> </Border> </DataTemplate> </Setter.Value> </Setter> </Style> </DataGridComboBoxColumn.EditingElementStyle> </DataGridComboBoxColumn> <DataGridTextColumn Header="ÅؽºÆ®" Binding="{Binding Text}" IsReadOnly="True"> <DataGridTextColumn.ElementStyle> <Style TargetType="TextBlock"> <Setter Property="Foreground"> <Setter.Value> <SolidColorBrush Color="{Binding Color}"/> </Setter.Value> </Setter> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn> </DataGrid.Columns> </DataGrid> </Grid> </Window>
using System.ComponentModel;
namespace BindingDemo { /// <summary> /// MainWindow.xaml¿¡ ´ëÇÑ »óÈ£ ÀÛ¿ë ³í¸® /// </summary> public partial class MainWindow : Window, INotifyPropertyChanged { private Color color = Colors.Black; public Color Color { get { return color; } set { color = value; OnPropertyChanged("Color"); } } public MainWindow() { InitializeComponent(); this.DataContext = this; } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string name) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } } public class TestClass : INotifyPropertyChanged { private Color color = Colors.Black; public Color Color { get { return color; } set { color = value; OnPropertyChanged("Color"); } } public string Text { get; set; } public TestClass() { } public event PropertyChangedEventHandler PropertyChanged = (s, e) => { }; private void OnPropertyChanged(string name) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } } } ´Ù¿î·Îµå : BindingDemo_DataGrid_Color.zip
Canvas ScaleÄÞº¸¹Ú½º¿¡¼ CanvasÀÇ ºñÀ²À» Á¶Á¤ÇÑ´Ù. <Window x:Class="BindingDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:BindingDemo" xmlns:s="clr-namespace:System;assembly=mscorlib" Title="BindingDemo" Height="380" Width="661" DataContext="{Binding}"> <Window.Resources> <x:Array x:Key="ScaleItems" Type="s:Int32"> <s:Int32>200</s:Int32> <s:Int32>100</s:Int32> <s:Int32>75</s:Int32> <s:Int32>50</s:Int32> <s:Int32>25</s:Int32> </x:Array> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition/> </Grid.RowDefinitions> <ComboBox ItemsSource="{StaticResource ScaleItems}" SelectedValue="{Binding Scale, UpdateSourceTrigger=PropertyChanged}" Width="70" Height="23" Margin="5" HorizontalAlignment="Left"/> <Canvas Grid.Row="1"> <Canvas.RenderTransform> <ScaleTransform ScaleX="{Binding CanvasScale}" ScaleY="{Binding CanvasScale}"/> </Canvas.RenderTransform> <Rectangle Canvas.Left="100" Canvas.Top="100" Width="100" Height="40" Fill="Red"/> <Ellipse Canvas.Left="200" Canvas.Top="30" Width="80" Height="80" Fill="Blue"/> </Canvas> </Grid> </Window>
using System.ComponentModel;
namespace BindingDemo { /// <summary> /// MainWindow.xaml¿¡ ´ëÇÑ »óÈ£ ÀÛ¿ë ³í¸® /// </summary> public partial class MainWindow : Window, INotifyPropertyChanged { public int Scale { get { return (int)CanvasScale * 100; } set { CanvasScale = value / 100.0; } } private double canvasScale = 1.0; public double CanvasScale { get { return canvasScale; } set { if (value > 0) { canvasScale = value; } OnPropertyChanged("CanvasScale"); } } public MainWindow() { InitializeComponent(); this.DataContext = this; } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string name) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } } } ´Ù¿î·Îµå : BindingDemo_Canvas_Scale.zip
Âü°í : http://cswpf.seesaa.net/index-9.html
|