데이터 바인딩 첫번째~
(DATA BINDING)
www.topcredu.co.kr 이종철
•
•
•
•
•
MainWindow.xaml
<Window x:Class="BindingLabelToScroll.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Label, ScrollBar 데이터 바인딩" Height="350" Width="525">
<StackPanel >
<!-- Source. -->
<ScrollBar Name="scrollbar"
Orientation="Horizontal" Margin="24"
Maximum="100" LargeChange="10" SmallChange="1" />
<!-- Target. -->
<Label HorizontalAlignment="Center"
Content="{Binding ElementName=scrollbar, Path=Value}" />
</StackPanel>
</Window>
실행 결과
바인딩 자체는 언제나 타겟에 설정한다. Label 컨트롤의 Content
프로퍼티에 다음과 같이 설정되었다.
<Label HorizontalAlignment="Center" Content="{Binding
ElementName=scrollbar, Path=Value}" />
Binding 키워드는 마크업 확장으로 중괄호안에 Binding 키워드가 있다.
Binding의 프로퍼티 중 ElementName에는 ScrollBar의 Name 속성에
정의된 이름이 사용되었고 Path 프로퍼티에는 ScrollBar의 Value
프로퍼티로 설정되었다.
앞 XAML에서 바인딩 정의 내에 따옴표를 사용하고 싶다면 아래과
같이 프로퍼티 엘리먼트를 사용하면 된다.
<!-- Target. -->
<Label HorizontalAlignment="Center">
<Label.Content>
<Binding ElementName="scrollbar" Path="Value"/>
</Label.Content>
</Label>
 바인딩 정의가 있는 컨트롤은 항상 타겟이다. 바인딩 타겟은
DependencyObject로부터 상속되며 바인딩이 설정되는 프로퍼티는
반드시 의존 프로퍼티의 지원을 받아야 한다. 따라서 이 경우에 Label은
DependencyProperty 타입의 public static 필드인 ContentProperty가
필요하다. (당연히 존재한다. 바인딩 소스는 반드시 의존 프로퍼티 일
필요는 없다.)
 프로퍼티는 프로퍼티가 변경 될 때 알려주는 이벤트와 연결되어 있는
것이 이상적이다. 하지만 몇몇 바인딩은 통지 이벤트 없이도 동작한다.
 바인딩을 C#코드로 구현한 예문을 보면 명확히 알 수 있는데 다음 페이지
코드를 보자.
MainWindow.xaml
<Window x:Class="BindingLabelToScroll2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Label, ScrollBar 데이터 바인딩" Height="350" Width="525">
<StackPanel >
<!-- Source. -->
<ScrollBar Name="scrollbar"
Orientation="Horizontal" Margin="24"
Maximum="100" LargeChange="10" SmallChange="1" />
<!-- Target. -->
<Label HorizontalAlignment="Center" Name="label"/>
</StackPanel>
</Window>
MainWindow.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Controls.Primitives;
namespace BindingLabelToScroll2 {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
Binding bind = new Binding();
bind.Source = scrollbar;
bind.Path = new PropertyPath(ScrollBar.ValueProperty);
label.SetBinding(Label.ContentProperty, bind);
}
}
}
소스와 타겟의 의미는 소스 엘리먼트에서의 변화를 타겟 엘리먼트에 반영되도록 하는 의미를 담고
있다. 시실 이것은 바인딩으로 가능한 4가지 모드 중 하나일 뿐이다. 바인딩 모드는 Mode 프로퍼티에
BindingMode 열거형 멤버로 설정할 수 있다.
Content = “{Binding ElementName=scrollbar, Path=Value, Mode=OneWay}”
<Label.Content>
<Binding ElementName=”scrollbar” Path=”Value” Mode=”OneWay”/>
</Label.Content>
Mode는 TwoWay로 설정할 수도 있는데 Label의 Content 프로퍼티의 변화도 scrollbar의 Value
프로퍼티에 반영된다.
OneTime 모드도 있는데 타겟이 소스로부터 초기화 되지만 소스의 변화가 계속 반영되지 않고 초기 한
번만 반영된다.
OneWayToSource 모드는 소스, 타겟의 의미와 반대가 되도록 타겟이 소스를 갱신하는 모양이다. 본
예제의 경우 Label은 ScrollBar에 넘겨줄 숫자 데이터가 없기에 Label은 비어 있고 ScrollBar를
움직여도 반응이 없는 형태가 된다.

(C#, WPF강좌)WPF, XAML 데이터바인딩이란? Data Binding 개요 및 예제_WPF학원/WPF교육

  • 1.
    데이터 바인딩 첫번째~ (DATABINDING) www.topcredu.co.kr 이종철
  • 2.
  • 3.
  • 4.
    MainWindow.xaml <Window x:Class="BindingLabelToScroll.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Label, ScrollBar데이터 바인딩" Height="350" Width="525"> <StackPanel > <!-- Source. --> <ScrollBar Name="scrollbar" Orientation="Horizontal" Margin="24" Maximum="100" LargeChange="10" SmallChange="1" /> <!-- Target. --> <Label HorizontalAlignment="Center" Content="{Binding ElementName=scrollbar, Path=Value}" /> </StackPanel> </Window>
  • 5.
  • 6.
    바인딩 자체는 언제나타겟에 설정한다. Label 컨트롤의 Content 프로퍼티에 다음과 같이 설정되었다. <Label HorizontalAlignment="Center" Content="{Binding ElementName=scrollbar, Path=Value}" /> Binding 키워드는 마크업 확장으로 중괄호안에 Binding 키워드가 있다. Binding의 프로퍼티 중 ElementName에는 ScrollBar의 Name 속성에 정의된 이름이 사용되었고 Path 프로퍼티에는 ScrollBar의 Value 프로퍼티로 설정되었다.
  • 7.
    앞 XAML에서 바인딩정의 내에 따옴표를 사용하고 싶다면 아래과 같이 프로퍼티 엘리먼트를 사용하면 된다. <!-- Target. --> <Label HorizontalAlignment="Center"> <Label.Content> <Binding ElementName="scrollbar" Path="Value"/> </Label.Content> </Label>
  • 8.
     바인딩 정의가있는 컨트롤은 항상 타겟이다. 바인딩 타겟은 DependencyObject로부터 상속되며 바인딩이 설정되는 프로퍼티는 반드시 의존 프로퍼티의 지원을 받아야 한다. 따라서 이 경우에 Label은 DependencyProperty 타입의 public static 필드인 ContentProperty가 필요하다. (당연히 존재한다. 바인딩 소스는 반드시 의존 프로퍼티 일 필요는 없다.)  프로퍼티는 프로퍼티가 변경 될 때 알려주는 이벤트와 연결되어 있는 것이 이상적이다. 하지만 몇몇 바인딩은 통지 이벤트 없이도 동작한다.  바인딩을 C#코드로 구현한 예문을 보면 명확히 알 수 있는데 다음 페이지 코드를 보자.
  • 9.
    MainWindow.xaml <Window x:Class="BindingLabelToScroll2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Label, ScrollBar데이터 바인딩" Height="350" Width="525"> <StackPanel > <!-- Source. --> <ScrollBar Name="scrollbar" Orientation="Horizontal" Margin="24" Maximum="100" LargeChange="10" SmallChange="1" /> <!-- Target. --> <Label HorizontalAlignment="Center" Name="label"/> </StackPanel> </Window>
  • 10.
    MainWindow.xaml.cs using System.Windows; using System.Windows.Controls; usingSystem.Windows.Data; using System.Windows.Controls.Primitives; namespace BindingLabelToScroll2 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Binding bind = new Binding(); bind.Source = scrollbar; bind.Path = new PropertyPath(ScrollBar.ValueProperty); label.SetBinding(Label.ContentProperty, bind); } } }
  • 11.
    소스와 타겟의 의미는소스 엘리먼트에서의 변화를 타겟 엘리먼트에 반영되도록 하는 의미를 담고 있다. 시실 이것은 바인딩으로 가능한 4가지 모드 중 하나일 뿐이다. 바인딩 모드는 Mode 프로퍼티에 BindingMode 열거형 멤버로 설정할 수 있다. Content = “{Binding ElementName=scrollbar, Path=Value, Mode=OneWay}” <Label.Content> <Binding ElementName=”scrollbar” Path=”Value” Mode=”OneWay”/> </Label.Content> Mode는 TwoWay로 설정할 수도 있는데 Label의 Content 프로퍼티의 변화도 scrollbar의 Value 프로퍼티에 반영된다. OneTime 모드도 있는데 타겟이 소스로부터 초기화 되지만 소스의 변화가 계속 반영되지 않고 초기 한 번만 반영된다. OneWayToSource 모드는 소스, 타겟의 의미와 반대가 되도록 타겟이 소스를 갱신하는 모양이다. 본 예제의 경우 Label은 ScrollBar에 넘겨줄 숫자 데이터가 없기에 Label은 비어 있고 ScrollBar를 움직여도 반응이 없는 형태가 된다.