VisualStudio/WPF

[WPF] 사용자 정의컨트롤 디펜던시프로퍼티(DependencyProperty)

usingsystem 2023. 1. 5. 14:21
728x90

디펜던시프로퍼티

  •  사용자 정의컨트롤을 만들어 사용 할 때 사용자 정의 컨트롤의 속성을 접근하여 get set을 하기 위한 프로퍼티 이다.

 

사용방법

  • 정의컨트롤 생성

 

  • 디펜던시 프로퍼티 생성 방법
    • PROPDP + Tab + Tab 하면 나오는 초기 디펜던시 프로퍼티
 public int MyProperty
        {
            get { return (int)GetValue(MyPropertyProperty); }
            set { SetValue(MyPropertyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyPropertyProperty =
            DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0));

DependencyProperty.Register("MyText", typeof(int), typeof(ownerclass), new PropertyMetadata(0));

                                              프로퍼티이름   /타입           /소유클레스            /기본값

 

  • Label Text 변경
    • 정의컨트롤 소스에서 사용프로 퍼티 정의
  public string MyText
        {
            get { return (string)GetValue(MyTextProperty); }
            set { SetValue(MyTextProperty, value); }
        }
        public static readonly DependencyProperty MyTextProperty =
            DependencyProperty.Register("MyText", typeof(string), typeof(ThreeControls), new PropertyMetadata(string.Empty));

 

  • 정의컨트롤 xaml에서 컨트롤 name추가 및 바인딩
    • x:Name="root" 추가
    • Content="{Binding MyText, ElementName=root}" 추가하여 바인딩
<UserControl x:Class="wpf_test.ThreeControls"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:wpf_test"
             mc:Ignorable="d" 
             d:DesignHeight="150" d:DesignWidth="300"
             x:Name="root"
             >
    <Grid>
        <Label x:Name="label" Content="{Binding MyText, ElementName=root}" HorizontalAlignment="Left" Margin="10,89,0,0" VerticalAlignment="Top"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="10,120,0,0" VerticalAlignment="Top"/>
    </Grid>
</UserControl>

 

  • 버튼이벤트 추가
    • 정의컨트롤 소스에서 사용프로 퍼티 정의
  public ICommand MyCommand
        {
            get { return (ICommand)GetValue(MyCommandProperty); }
            set { SetValue(MyCommandProperty, value); }
        }
        public static readonly DependencyProperty MyCommandProperty =
            DependencyProperty.Register("MyCommand", typeof(ICommand), typeof(ThreeControls));

 

  • 정의컨트롤 xaml에서 컨트롤  클릭 바인딩
    • Command="{Binding MyCommand, ElementName=root}" 추가하여 바인딩
  <Button x:Name="button" Command="{Binding MyCommand, ElementName=root}" Content="Button" HorizontalAlignment="Left" Margin="10,120,0,0" VerticalAlignment="Top"/>

 

  • View에서 사용 방법
    • 위에서 생성한 디펜던시 프로퍼티를 추가 하여 사용한다
      • (ex)  MyText="112323" MyCommand="{Binding TestClick}"
   <local:ThreeControls HorizontalAlignment="Left" Margin="28,76,0,0" Grid.Row="1" VerticalAlignment="Top" MyText="112323" MyCommand="{Binding TestClick}" />
728x90