UserControl을 사용하다 보면,
그 컨트롤을 적용하는 부모 컨트롤에서 해당 값들을 제어해줘야 할 경우가 생기는데요.
이때 UserControl의 내부 값을 직접 제어 해준다고 생각하면, 좀 복잡해 집니다.
그래서 나온게 의존 프로퍼티 인듯 합니다.
우선 UserControl("사용자 정의 컨트롤")을 하나 생성합니다.
그리고 해당 프로퍼티 값을 노출 시킵니다.
이 과정은 msdn에도 많은 부분 할애되어 있습니다. "의존 프로퍼티"라고도 합니다.
public string FileName
{
get
{
return (string)GetValue(FileNameProperty);
}
set
{
SetValue(FileNameProperty, value);
}
}
public static DependencyProperty FileNameProperty =
DependencyProperty.Register("FileName", typeof(string), typeof(UserControl1));
위의 코드를 통해서 FileName 라는 프로퍼티가 노출 된다고 보시면 됩니다.
내부적으로 돌아가는거나, 여러가지 책을 통한 내용들은 왠지 모르게 어렵네욤.. 전.. @.@.
이렇게 작성한 후에 UserControl을 가져다 쓰는 경우는 아래와 같습니다.
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:local="clr-namespace:WpfApplication2"
Title="Window1" Height="300" Width="396">
<StackPanel>
<local:UserControl1 x:Name="testUC" FileName="c:\lindsay.htm"/>
<TextBlock Text="{Binding FileName, ElementName=testUC}"/>
</StackPanel>
</Window>
네임 스페이스 추가 및 UserControl을 사용하고 있습니다. (화살표 부분 참조)
하나의 새로운 컨트롤이 생성되면서, 다른 컨트롤들과의 데이터 바인딩이 가능하게 되는것
그것이 의존 프로퍼티의 장점이라고 생각 됩니다.
물론 프로퍼티 내부에서는 해당 컨트롤의 각종 내용들을 처리해주면 되겠죠..
이렇게 의존 프로퍼티를 사용하게 되면 VS 또는 Blend에 해당 정보를 설정할 수 있도록 노출 되게 됩니다.
전체 소스 코드 :
[Window1.xaml]
더보기
<Window x:Class="WpfApplication2.Window1"
xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:local="clr-namespace:WpfApplication2"
Title="Window1" Height="300" Width="396">
<StackPanel>
<local:UserControl1 x:Name="testUC" FileName="c:\lindsay.htm"/>
<TextBlock Text="{Binding FileName, ElementName=testUC}"/>
</StackPanel>
</Window>
[Window1.xaml.cs]
더보기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication2
{
/// <summary>
/// Window1.xaml에 대한 상호 작용 논리
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
}
[UserControl1.xaml]
더보기
<UserControl x:Class="WpfApplication2.UserControl1"
xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml "
Height="39" Width="300" x:Name="root">
<DockPanel>
<Button x:Name="theButton" DockPanel.Dock="Right" Click="theButton_Click">
Browse...
</Button>
<TextBox x:Name="theTextBox"
MinWidth="{Binding ActualWidth, ElementName=theButton}" Margin="0,0,2,0"
Text="{Binding FileName, ElementName=root}"/>
</DockPanel>
</UserControl>
[UserControl1.xaml.cs]
더보기
using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
namespace WpfApplication2
{
/// <summary>
/// UserControl1.xaml에 대한 상호 작용 논리
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
theTextBox.TextChanged += new TextChangedEventHandler(theTextBox_TextChanged);
}
void theTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
e.Handled = true;
if (FileNameChanged != null)
FileNameChanged(this, EventArgs.Empty);
}
private void theButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog d = new OpenFileDialog();
if (d.ShowDialog() == true)
this.FileName = d.FileName;
}
public string FileName
{
get
{
return (string)GetValue(FileNameProperty);
}
set
{
SetValue(FileNameProperty, value);
}
}
public static DependencyProperty FileNameProperty =
DependencyProperty.Register("FileName", typeof(string), typeof(UserControl1));
public event EventHandler<EventArgs> FileNameChanged;
}
}
크리에이티브 커먼즈 라이선스