µ¥ÀÌÅÍ ¹ÙÀεù 6

Æ®¸®°Å¸¦ ÅëÇØ XAML¿¡¼­ ±âº»ÀûÀΠUI Á¦¾î°¡ °¡´ÉÇÏ´Ù.

Play ¹öÆ°À» Ŭ¸¯Çϸé Stop ¹öÆ°ÀÌ È°¼ºÈ­ µÇ°í Play ¹öÆ°ÀÌ ºñÈ°¼ºÈ­ µÇ°í
Stop ¹öÆ°À» Ŭ¸¯Çϸé Play ¹öÆ°ÀÌ È°¼ºÈ­ µÇ°í Stop ¹öÆ°ÀÌ ºñÈ°¼ºÈ­ µÇ´Â °ÍÀ» ¸¸µé¾î º¸ÀÚ.

Button.StyleÀ» ÄÁÆ®·Ñ´ç Çϳª¾¿ ¹èÄ¡ÇÏ°í ÀÖ´Ù.
Æ®¸®°Å¸¦ StyleÀÇ Style.Triggers ³»ºÎ¿¡ ÀÛ¼ºÇÑ´Ù.

<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¸¦ ½ÇÇàÇÑ´Ù.
¼Ó¼º °ªÀÌ Value¿Í ´Ù¸£¸é Style.Triggers  ¹ÛÀÇ Setter¸¦ ½ÇÇàÇÑ´Ù.
Style.Triggers ¿ÜºÎ¿¡ Setter¸¦ ÀÛ¼ºÇÏÁö ¾ÊÀ¸¸é ±âº»°ªÀÌ´Ù.

                            <DataTrigger Binding="{Binding IsRunning}" Value="True">
                                <Setter Property="IsEnabled" Value="False"/>
                            </DataTrigger>

Play ¹öÆ°Àº IsRunningÀÌ TrueÀ϶§ ¹öÆ°ÀÇ IsEnabled´Â FlalseÀÌ´Ù.
Play ¹öÆ°Àº IsRunningÀÌ FalseÀ϶§ ¹öÆ°ÀÇ IsEnabled´Â TrueÀÌ´Ù.

                            <DataTrigger Binding="{Binding IsRunning}" Value="True">
                                <Setter Property="IsEnabled" Value="True"/>
                            </DataTrigger>

Stop ¹öÆ°Àº IsRunningÀÌ TrueÀ϶§ ¹öÆ°ÀÇ IsEnabled´Â TrueÀÌ´Ù.
Stop ¹öÆ°Àº IsRunningÀÌ FalseÀ϶§ ¹öÆ°ÀÇ IsEnabled´Â FalseÀÌ´Ù.

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( ) ¸Þ½îµå°¡ ½ÇÇàµÈ´Ù.
Stop ¹öÆ°À» Ŭ¸¯Çϸé Stop_Click( ) ¸Þ½îµå°¡ ½ÇÇàµÈ´Ù.

´Ù¿î·Îµå : BindingDemo_trigger.zip

 

ÄÞº¸¹Ú½º¿¡¼­ »ö»ó ¼±ÅÃÇϱâ

ÄÞº¸¹Ú½º¿¡¼­ Ä®¶ó¸¦ ¼±ÅÃÇÏ¸é ¿·ÀÇ ÅؽºÆ®¿¡ Ç¥½ÃÇÑ´Ù.

ÄÞº¸¹Ú½º¿¡¼­ Ä®¶ó¸¦ ¼±ÅÃÇÏ¸é ¹ÙÀεù µÈ  "Color"¿¡ ÀÇÇؼ­ TextBlockÀº ¹®ÀÚ¿­À» "Color" »ö»óÀ¸·Î ¹Ù²Û´Ù.
¼Ò½º´Â INotifyPropertyChanged¿¡ ÀÇÇØ ¹ÙÀεù µÈ ¼Ó¼º¸¦ ¾÷µ¥ÀÌÆ® ÇÏ´Â °Í¹Û¿¡ ¾ø´Ù.

<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ÀÇ ºñÀ²À» Á¶Á¤ÇÑ´Ù.
ºñÀ²Àº Canvas.RenderTransformÀÇ ScaleTransoformÀ¸·Î ¼³Á¤ÇÑ´Ù.
¼³Á¤ °ªÀº 0.0 ~ 1.0 ÀÌ´Ù. ÄÞº¸¹Ú½º´Â % °ªÀ̹ǷΠ¼Ò½ºÀÇ ¼Ó¼º¿¡¼­ %¿Í ºñÀ²·Î »óÈ£ º¯È¯ÇÑ´Ù.

<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