상세 컨텐츠

본문 제목

[Silverlight] Element to Element Binding

Web Development/Silverlight

by thankee 2009. 11. 16. 21:27

본문

Silverlight 3에서 새로 추가된 기능 중 하나가 바로 이 Element to Element Bidning입니다. 이 기능은 특정 Element의 속성 값을 다른 Element에서 Binding을 통해 사용할 수 있도록 하는 기능입니다. 다음의 예제를 본다면 쉽게 이해하실 수 있습니다.

예제

위의 예제를 보시면 CheckBox Control이 TextBox Control을 활성화 시키고, TextBox Control의 내용이 TextBlock의 크기나 색을 변화시키는 것을 확인하실 수 있습니다.

위 예제는 C#코드를 이용하지 않고 모두 Element To Element Binding를 이용한 예제입니다.이 기능을 CodeBehind에서 C#으로 구현하기 위해서는 보통 컨트롤별로 Event를 생성하고 수십줄의 코드를 작성해야하지만 Element To Element Binding를 이용하면 아래 소스 처럼 간편하게 직관적으로 코드를 작성하실 수 있습니다.

소스코드

<StackPanel x:Name="LayoutRoot">
    <StackPanel Orientation="Horizontal">
        <CheckBox x:Name="chkEnable" Content="입력하기" />
        <TextBlock Text="    텍스트 크기 입력 : " FontWeight="Bold" />
        <TextBox x:Name="tbxAmount" IsEnabled="{Binding IsChecked, ElementName=chkEnable}" Width="100" />
        <TextBlock Text="    텍스트 색 입력 : " FontWeight="Bold" />
        <TextBox x:Name="tbxColor" IsEnabled="{Binding IsChecked, ElementName=chkEnable} Width="100" />
    </StackPanel>
    <Border BorderBrush="Silver" BorderThickness="3">
        <TextBlock FontSize="{Binding Text, ElementName=tbxAmount}" Foreground="{Binding Text, ElementName=tbxColor}" Text="텍스트 크기" />
    </Border>
</StackPanel>

위의 코드에서 TextBox와 TextBlock의 각 부분에 존재하는 모든 Binding 구문이 Element To Element Binding을 사용하고 있습니다. 소스처럼 {Binding Property명, ElementName=Element명}으로 간단하게 Element To Element Binding를 사용하실 수 있습니다.

이 기능은 DataGrid에서 DataTemplate내부의 컨트롤이 부모 DataGrid를 참조할 수 없다는 제약이 있습니다. 이 문제는 공개적으로 Issue이며 언젠가는 수정되리라 믿고 있습니다...

관련글 더보기