Dreamine.MVVM.Behaviors.Core
Dreamine 프레임워크에서 WPF MVVM Behavior 구현을 위한 핵심 인프라 라이브러리입니다.
이 패키지는 WPF UI 요소에 재사용 가능한 Behavior를 구현하기 위한 기본 구조를 제공합니다.
상위 패키지들의 기반 레이어로 사용됩니다.
예:
- Dreamine.MVVM.Behaviors
- Dreamine.MVVM.Triggers
- Dreamine.MVVM.Interactions
➡️ English Version
목적
WPF Behavior는 UI 로직을 View에 직접 작성하지 않고
MVVM 패턴을 유지하면서 UI 확장 기능을 제공할 수 있게 합니다.
하지만 기존 Behavior 구현 방식은 다음 문제들이 있습니다.
- 강한 프레임워크 의존성
- 암묵적인 초기화
- 타입 안정성 부족
Dreamine.MVVM.Behaviors.Core는 다음 목표로 설계되었습니다.
- 최소 의존성
- 명시적 Attach/Detach 구조
- Strongly Typed Behavior
핵심 개념
Behavior
제네릭 기반 Behavior 기본 클래스입니다.
특징:
AssociatedObject타입 안정성 제공Freezable기반 XAML 지원- Attach / Detach 라이프사이클 관리
- MVVM 친화적 구조
사용 대상 예:
- Window
- Button
- TextBox
- Grid
IAttachedObject
DependencyObject에 연결되는 객체의 공통 인터페이스입니다.
역할:
- 연결된 UI 요소 관리
- 연결 및 해제 라이프사이클 제공
메서드:
Attach(DependencyObject)
Detach()
IBehavior
Dreamine Behavior의 핵심 인터페이스입니다.
모든 Behavior는 이 인터페이스 기반으로 구현됩니다.
프로젝트 구조
Dreamine.MVVM.Behaviors.Core
│
├─ Base
│ └─ BehaviorGeneric.cs
│
├─ Interfaces
│ ├─ IAttachedObject.cs
│ └─ IBehavior.cs
│
└─ Dreamine.MVVM.Behaviors.Core.csproj
아키텍처
WPF UI Element
│
└─ Behavior<T>
│
├─ Attach()
└─ Detach()
이 구조를 통해 UI 확장 기능을 구현하면서도
ViewModel 로직과 완전히 분리된 구조를 유지할 수 있습니다.
예제
Behavior 기본 구조:
public class FocusBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
AssociatedObject.Focus();
}
protected override void OnDetaching()
{
}
}
XAML 사용 예:
<TextBox>
<i:Interaction.Behaviors>
<local:FocusBehavior/>
</i:Interaction.Behaviors>
</TextBox>
요구사항
.NET 8.0
WPF
관련 패키지
- Dreamine.MVVM.Behaviors
- Dreamine.MVVM.Attributes
- Dreamine.MVVM.ViewModels
License
MIT License
구조 다이어그램
classDiagram
class EventToCommandBehavior {
+string EventName
+IRelayCommand Command
+object CommandParameter
#OnAttached() void
-OnEventRaised(object, EventArgs) void
}
class DoubleClickBehavior {
+IRelayCommand Command
+object CommandParameter
#OnAttached() void
}
class KeyBindingBehavior {
+Key Key
+ModifierKeys Modifiers
+IRelayCommand Command
#OnAttached() void
}
class ValidationBehavior {
+string PropertyName
+string ErrorMessage
#OnAttached() void
+Validate() bool
}
class BehaviorBase~T~ {
<<abstract>>
}
BehaviorBase~T~ <|-- EventToCommandBehavior
BehaviorBase~T~ <|-- DoubleClickBehavior
BehaviorBase~T~ <|-- KeyBindingBehavior
BehaviorBase~T~ <|-- ValidationBehaviorAPI 문서
타입
\if KO 📌 Dreamine의 제네릭 기반 Behavior 클래스입니다. 연결 가능한 대상 객체를 제네릭 타입 로 명확히 지정하여, AssociatedObject를 타입 캐스팅 없이 직접 사용할 수 있도록 도와줍니다. Freezable을 상속하여 WPF의 리소스 시스템 및 XAML 지원을 보장하며, IAttachedObject 인터페이스를 통해 Behavior의 연결/해제를 관리합니다. \endif \if EN Encapsulates behavior functionality and related state. \endif
\if KO 📌 Dreamine의 모든 Behavior는 이 인터페이스를 통해 연결 객체를 관리합니다. FrameworkElement 또는 DependencyObject와 연결되는 객체의 공통 인터페이스로, Behavior 및 트리거 확장 구현 시 연결 대상의 참조를 일관되게 제공합니다. \endif \if EN Encapsulates i attached object functionality and related state. \endif
\if KO 📌 Dreamine에서 모든 Behavior가 구현해야 하는 핵심 인터페이스입니다. - View 요소(DependencyObject)에 연결/분리되는 확장 동작을 정의합니다. - XAML 또는 코드에서 동적으로 attach 가능한 구조 기반입니다. \endif \if EN Encapsulates i behavior functionality and related state. \endif
Behavior`1
\if KO Behavior를 지정한 에 연결합니다. 내부적으로 에 저장되며, OnAttached()가 호출됩니다. \endif \if EN Attaches the behavior to a target object. \endif
dependencyObject— \if KO 연결할 DependencyObject 입니다. (예: Window, Grid 등) \endif \if EN The dependency object to which the behavior or attached property applies. \endif\if KO WPF Freezable 객체를 생성하기 위한 팩토리 메서드입니다. Activator.CreateInstance()를 사용하므로 기본 생성자가 반드시 필요합니다. \endif \if EN Creates the instance core value. \endif
반환: \if KO 현재 클래스 타입의 새로운 인스턴스 \endif \if EN The result produced by the create instance core operation. \endif
\if KO 현재 연결된 객체와의 연결을 해제합니다. OnDetaching()이 호출되고, AssociatedObject는 null로 초기화됩니다. \endif \if EN Detaches the behavior from its current target object. \endif
\if KO Behavior가 연결되었을 때 호출되는 확장 포인트입니다. 하위 클래스에서 오버라이딩하여 부가 작업을 수행할 수 있습니다. \endif \if EN Handles the attached event or state change. \endif
\if KO Behavior가 해제될 때 호출되는 확장 포인트입니다. 하위 클래스에서 오버라이딩하여 정리 작업 등을 수행할 수 있습니다. \endif \if EN Handles the detaching event or state change. \endif
\if KO 연결된 WPF 객체를 나타냅니다. Behavior가 Attach되면 이 속성에 대상 객체가 설정됩니다. \endif \if EN Gets or sets the associated object value. \endif
\if KO 인터페이스를 통해 노출되는 AssociatedObject입니다. \endif \if EN Gets the associated object value. \endif
IAttachedObject
\if KO 지정된 DependencyObject에 현재 객체를 연결합니다. \endif \if EN Attaches the behavior to a target object. \endif
dependencyObject— \if KO 연결할 대상 객체입니다. \endif \if EN The dependency object to which the behavior or attached property applies. \endif\if KO 현재 객체를 연결된 대상에서 분리합니다. \endif \if EN Detaches the behavior from its current target object. \endif
\if KO 연결된 객체 (DependencyObject)를 반환합니다. \endif \if EN Gets the associated object value. \endif