레이블이 #의존속성인 게시물을 표시합니다. 모든 게시물 표시
레이블이 #의존속성인 게시물을 표시합니다. 모든 게시물 표시

2019년 1월 26일 토요일

WPF교육)의존프로퍼티, (DependencyProperty), 의존속성 이란?

WPF교육)의존프로퍼티, (DependencyProperty), 의존속성 이란?
의존 프로퍼티(DependencyProperty), 의존속성
n 기존의 닷넷 Property에 WPF 요소를 가미하여 새롭게 탄생 시킴
n XAML, C# 코드 비하인드에서 사용 가능하며 의존속성 값이 변경되면 자동으로 어떤 것을 로드되게 하거나 랜더링 되도록 할 수 있는데 애니메이션, 스타일링, 데이터바인딩 등에 자주 사용된다.
n 우선 간단히 예제를 하나 만들어 보자.
n 비주얼 스튜디오 -> WPF 응용프로그램 , 프로젝트명 : DependencyPropertyTest
MainWindow.xaml
<Window x:Class="DependencyPropertyTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="295" Width="300">
<Window.ContextMenu>
<ContextMenu MenuItem.Click="ContextMenu_Click">
<MenuItem Header="Red"/>
<MenuItem Header="Green"/>
<MenuItem Header="Blue"/>
<MenuItem Header="Orange"/>
</ContextMenu>
</Window.ContextMenu>
<TextBox x:Name="textBox1" Height="23" TextWrapping="Wrap" Text="TextBox" Width="120"/> </Window>
MainWindow.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace DependencyPropertyTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public String MyText
{
get { return (String)GetValue(MyProperty); }
set { SetValue(MyProperty, value); }
}
public static readonly DependencyProperty MyProperty = DependencyProperty.Register(
"MyText", //등록할 의존성 속성 이름
typeof(String),
typeof(MainWindow),
new FrameworkPropertyMetadata(new PropertyChangedCallback(OnMyPropertyChanged)));
private static void OnMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MainWindow win = d as MainWindow;
SolidColorBrush brush = (SolidColorBrush)new BrushConverter().ConvertFromString(e.NewValue.ToString());
win.Background = brush;
win.Title = (e.OldValue == null) ? "제목없음" : e.OldValue.ToString();
win.textBox1.Text = e.NewValue.ToString();
}
private void ContextMenu_Click(object sender, RoutedEventArgs e)
{
string str = (e.Source as MenuItem).Header as string;
MyText = str;
}
}
}
n 실행화면
n 의존 속성은 의존속성을 선언, 등록 그리고 프로퍼티를 생성하는 3단계로 작성된다.
n 의존 속성 선언 및 등록
//의존 속성 선언 및 등록
public static readonly DependencyProperty MyProperty = DependencyProperty.Register(
" MyText", //등록할 의존 속성 이름
typeof(String),
typeof(MainWindow),
new FrameworkPropertyMetadata(new PropertyChangedCallback(OnMyPropertyChanged))); //속성변경시 호출될 메소드
의존속성은 읽기전용(readonly) 필드로 선언되는데 이것은 오직 FrameworkElement 클래스의 static 생성자에서만 설정될 수 있다는 것을 의미한다.
DependencyProperty 클래스에는 public 생성자가 없기 때문에 static 메소드인 DependencyProperty.Register()를 사용해서 등록한다.
Register 메서드의 입력 파라미터의 첫 번째 파라미터는 프로퍼티 이름이다. 여기서는 “MyText”, 두 번째 인자는 프로퍼티(MyText)가 사용할 데이터 타입으로 여기에서는 String 이다. 세 번째 인자는 프로퍼티를 소유하게 될 타입, 이 예제에서는 MainWindow 클래스가 된다. 네 번째 인자는 실제로 어떻게 동작할 것인지에 대한 옵션을 설정을 할당해 준다. FrameworkPropertyMetadata 객체를 통하여 만약 값이 수정되었을 때의 알림을 어떻게 받을 것인가를 정의했으며 본 예제에서는 OnMyPropertyChanged 메소드가 알림을 받을 콜백 함수로 정의되었다. 선택적으로new ValidateValueCallback을 사용하여 값의 유효성 검사를 어떻게 할 것인지 등을 설정하면 된다. 네 번째, 다섯 번째 파라미터는 옵션 파라미터이다.
n DependencyProperty(MyProperty)를 위한 래퍼 프로퍼티 MyText 선언
public String MyText
{
get { return (String)GetValue(MyProperty); }
set { SetValue(MyProperty, value); }
}
이 래퍼 프로퍼티에서는 System.Windows.DependencyObject 클래스의 GetValue()와 SetValue() 메서드를 이용해서 get, set을 정의해야한다.
n Context Menu Click 이벤트에서는 MyText 프로퍼티에 값을 설정하면 자동으로 위에서 선언한 콜백 함수(OnMyPropertyChanged)가 호출된다.
private void ContextMenu_Click(object sender, RoutedEventArgs e)
{
string str = (e.Source as MenuItem).Header as string;
MyText = str;
}
n 우리가 흔히 알고 있는 Height와 Width와 같은 멤버들은 FrameworkElement를 상속받았고 Content 속성은 ControlContent로부터 상속받은 속성으로 모두 의존속성이다.
n 데이터 바인딩에서 의존속성을 사용하는 예제를 작성하자.
n 비주얼 스튜디오 -> WPF 응용프로그램 , 프로젝트명은 WpfApp1
DPTest.cs
using System.Diagnostics;
using System.Windows;
namespace WpfApp1
{
public class DPTest : DependencyObject
{
//의존 속성을 정의
public static readonly DependencyProperty TestProperty = DependencyProperty.Register(
"Test",
typeof(string),
typeof(DPTest),
new PropertyMetadata("Dependency Property Initial Value", OnTestPropertyChanged)); //Test 속성의 값이 바뀌면 OnTestPropertyChanged 호출
//TestProperty라는 의존속성을 래핑하고 있는 일반 속성,
//이값이 바뀜을 통지 받아서 OnTestPropertyChanged 메소드가 호출된다.
public string Test
{
get
{
Debug.WriteLine("Test GetValue");
return (string)GetValue(TestProperty);
}
set
{
Debug.WriteLine("Test SetValue");
SetValue(TestProperty, value);
}
}
private static void OnTestPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine("Property Changed : {0}", e.NewValue);
}
}
}
MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<TextBlock>의존 속성 테스트</TextBlock>
<TextBox Text="{Binding Path=Test,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<TextBox.DataContext>
<local:DPTest/>
</TextBox.DataContext>
</TextBox>
</StackPanel>
</Window>
n 실행화면

(C#교육동영상)C# ADO.NET 실습 ODP.NET/ODAC 설치 오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원

  (C#교육동영상)C# ADO.NET 실습  ODP.NET/ODAC 설치  오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원 https://www.youtube.com/watch?v=qIPU85yAlzc&list=PLxU-i...