본문 바로가기
→ 프로그래밍.데이터베이스/MFC,C,WPF

[WPF] Data Binding이란?

by jjo_Mean 2016. 10. 14.

 

WPF Data Binding에 대해서 알아보자.

 WPF Data Binding이란?

- 응용 프로그램의 UI 컨트롤의 속성을 다른 컨트롤의 속성이나 어떠한 개체의 속성값으로 자동으로 변하게 하는 등의 작업을 할 수 있는 것.

 

* 바인딩 타겟

•   개체 :  변하길 기대 하는 요소

•   속성 :  실제 변화가 생기는 값

 

* 바인딩 소스

•   개체 :  제공 될 요소

•   속성 :  제공 될 값

 

* 바인딩의 모드 4가지

소스 요소의 변화를 타겟 요소에 반영한다.

1. OneWay (default)  - 소스 엘리먼트에서의 변화를 타겟 엘리먼트에 반영

2. TwoWay  (양방향)  - OneWay+타겟 엘리먼트의 변화를 소스 엘리먼트엗 반영

3. OneTime  (초기화 시) – 타겟이 소스로부터 초기화되지만 소스의 변화에 반응하지 않는다.

                       – 한번만 초기화 되고 더 이상 되지 않는다.

4. OneWayToSource (역방향) – 타겟 엘리먼트의 변화를 소스 엘리먼트에 반영.

 



[사용예제]

      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

      Title="MainWindow" Height="336" Width="532"

DataContext="{Binding ElementName=scroll}">

 

   <Grid Height="240">

       <Grid.ColumnDefinitions>

           <ColumnDefinition Width="*"/>

           <ColumnDefinition Width="*"/>  

                         

       </Grid.ColumnDefinitions>

       

       <Grid.RowDefinitions>

           <RowDefinition Height="Auto"/>

           <RowDefinition Height="Auto"/>

           <RowDefinition Height="Auto"/>

           <RowDefinition Height="Auto"/>

           <RowDefinition Height="141*" />

       </Grid.RowDefinitions>

<Label Margin="24,12" Content="Source TextBox Controls" />

<Label Grid.Row="0" Grid.Column="1" Margin="20,24,27,0" Content="Target TextBox Controls"

/>

<TextBox Grid.Row="1" Grid.Column="0" Name="textbox1" Margin="24"/>

       <TextBox Grid.Row="1" Grid.Column="1" Margin="24"

T


Text="{Binding ElementName=textbox1, Path=Text, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>

 

       <TextBox Grid.Row="2" Grid.Column="0" Name="textbox2" Margin="24"/>

       <TextBox Grid.Row="2" Grid.Column="1" Margin="24" Text="{BindingElementName=textbox2, Path=Text, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>

 

       <TextBox Grid.Row="3" Grid.Column="0" Name="textbox3" Margin="24"/>

       <TextBox Grid.Row="3" Grid.Column="1" Margin="24" Text="{BindingElementName=textbox3, Path=Text, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>


   </Grid>

</Window>




[ 실행화면 ]