Dreamine.MVVM.Behaviors
WPF 환경에서 MVVM 기반 입력 처리와 포커스 제어를 단순하게 구성하기 위한 경량 Behavior 패키지입니다.
이 라이브러리는 반복적으로 발생하는 UI 상호작용을 코드 비하인드 대신 XAML + ICommand 중심 구조로 정리하는 용도에 맞춰져 있습니다.
개요
Dreamine.MVVM.Behaviors는 WPF MVVM 애플리케이션에서 자주 필요한 UI 동작을 Behavior 형태로 분리한 모듈입니다.
현재 패키지는 다음 두 가지 동작을 제공합니다.
- 사용자가 Enter 키를 눌렀을 때 ViewModel의 명령 실행
- 컨트롤이 로드된 직후 자동 포커스 설정
이 모듈은 .NET 8 WPF를 대상으로 하며, Dreamine MVVM 생태계 안에서 사용할 수 있도록 구성되어 있습니다. 프로젝트 파일 기준 패키지 정보는 Dreamine.MVVM.Behaviors, 버전 1.0.2입니다.
포함 기능
1. EnterKeyCommandBehavior
UIElement 대상 Attached Behavior입니다.
역할:
KeyDown이벤트 감지Key.Enter입력 확인- 연결된
ICommand실행 - 실행 후 이벤트 처리 완료 표시
사용 예:
- 로그인 입력창 엔터 실행
- 검색창 엔터 실행
- 텍스트 입력 후 확인 동작
동작 요약:
- Attached Property:
Command - Command Parameter: 현재
null CanExecute(null)가true일 때만 실행
2. FocusOnLoadedBehavior
FrameworkElement 대상 Attached Behavior입니다.
역할:
- 컨트롤 로드 완료 시점 감지
- 대상 요소에 자동으로 키보드 포커스 부여
사용 예:
- 로그인 화면 첫 입력칸 포커스
- 검색창 자동 포커스
- 입력 폼 초기 커서 위치 지정
동작 요약:
- Attached Property:
IsEnabled - 아래 조건을 만족할 때만 포커스 적용
FocusableIsEnabled == trueVisibility == Visible
설치
NuGet
dotnet add package Dreamine.MVVM.Behaviors
PackageReference
<PackageReference Include="Dreamine.MVVM.Behaviors" Version="1.0.2" />
요구사항
- .NET:
net8.0-windows - WPF 활성화 필요
- Dreamine Behavior 기반 의존성:
Dreamine.MVVM.Behaviors.Core
프로젝트 구조
Dreamine.MVVM.Behaviors/
├─ Dreamine.MVVM.Behaviors.csproj
├─ MVVM/
│ ├─ EnterKeyCommandBehavior.cs
│ └─ FocusOnLoadedBehavior.cs
└─ README.md
Quick Start
EnterKeyCommandBehavior
<Window x:Class="Sample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mvvm="clr-namespace:Dreamine.MVVM.Behaviors.MVVM;assembly=Dreamine.MVVM.Behaviors">
<Grid>
<TextBox Width="240"
mvvm:EnterKeyCommandBehavior.Command="{Binding LoginCommand}" />
</Grid>
</Window>
예시 ViewModel:
public sealed class LoginViewModel
{
public ICommand LoginCommand { get; }
public LoginViewModel()
{
LoginCommand = new RelayCommand(OnLogin);
}
private void OnLogin()
{
// 로그인 처리
}
}
FocusOnLoadedBehavior
<Window x:Class="Sample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mvvm="clr-namespace:Dreamine.MVVM.Behaviors.MVVM;assembly=Dreamine.MVVM.Behaviors">
<Grid>
<TextBox Width="240"
mvvm:FocusOnLoadedBehavior.IsEnabled="True" />
</Grid>
</Window>
설계 의도
이 패키지는 다음 방향을 따른다.
- Behavior를 작게 유지한다
- XAML 사용 방식을 명시적으로 유지한다
- 불필요한 추상화를 추가하지 않는다
- 코드 비하인드로 빠지는 UI 로직을 MVVM 구조 안으로 끌어온다
즉, 재사용 가능한 UI 모듈에서 반복되는 상호작용 코드를 표준화하는 데 적합하다.
정밀 분석 결과
압축 파일 내부 소스를 기준으로 확인된 내용은 다음과 같다.
- 실제 포함된 Behavior는 2개뿐이다.
- 기존 README에 적힌 이름은 실제 클래스명과 일치하지 않는다.
- 잘못된 표기:
MVVMEntryCommandBehavior - 실제 클래스명:
EnterKeyCommandBehavior - 잘못된 표기:
MVVMFocusOnLoadedBehavior - 실제 클래스명:
FocusOnLoadedBehavior
- 잘못된 표기:
- 프로젝트 파일 기준 패키지 버전은 1.0.2이다.
FocusOnLoadedBehavior는 Attached Property 기반Loaded처리 경로 하나만 사용한다.
즉, README는 예전 문구가 아니라 실제 코드 기준으로 다시 작성하는 것이 맞다.
한계
현재 구현 기준 한계는 다음과 같다.
EnterKeyCommandBehavior는 CommandParameter를 전달하지 않는다EnterKeyCommandBehavior는KeyDown만 처리한다FocusOnLoadedBehavior는 Attached Property 경로를 통해 로드 시점 1회 포커스만 처리한다- 이벤트 인자 전달, 지연 포커스, 선택적 포커스 라우팅 같은 고급 기능은 아직 없다
권장 개선 사항
CommandParameter지원 추가- Enter 고정이 아니라 Key 선택형 옵션 추가
- Dispatcher 기반 지연 포커스 옵션 추가
- 텍스트 입력용
SelectAllOnFocusBehavior 추가 - XML 주석 / README / 패키지 메타데이터 동기화 정리
저장소 메타데이터
- Package ID:
Dreamine.MVVM.Behaviors - Version:
1.0.2 - License:
MIT - Repository:
https://github.com/CodeMaru-Dreamine/Dreamine.MVVM.Behaviors
License
LICENSE 파일 참고.
구조 다이어그램
classDiagram
class IBehavior {
<<interface>>
+Attach(object) void
+Detach() void
+IsAttached bool
}
class IBehavior~T~ {
<<interface>>
+AssociatedObject T
+Attach(T) void
}
class BehaviorBase {
<<abstract>>
+IsAttached bool
+Attach(object) void
+Detach() void
#OnAttached() void
#OnDetaching() void
}
class BehaviorBase~T~ {
<<abstract>>
+AssociatedObject T
#OnAttached() void
#OnDetaching() void
}
class BehaviorCollection {
+Add(IBehavior) void
+Remove(IBehavior) void
+AttachAll(object) void
+DetachAll() void
}
IBehavior <|-- IBehavior~T~
IBehavior <|.. BehaviorBase
IBehavior~T~ <|.. BehaviorBase~T~
BehaviorBase <|-- BehaviorBase~T~
BehaviorCollection o-- IBehaviorAPI 문서
타입
\if KO 사용자가 Enter 키를 누를 때 지정된 ICommand를 실행하는 Behavior입니다. MVVM 구조에서 ViewModel의 명령(예: 로그인, 검색 실행 등) TextBox 또는 Input 계열 컨트롤에서 직접 트리거할 수 있도록 연결합니다. \endif \if EN Encapsulates enter-key command behavior functionality and related state. \endif
\if KO 📌 컨트롤이 로드되면 자동으로 포커스를 설정하는 Behavior입니다. 주로 로그인 화면, 검색창, 입력 폼 등에서 첫 포커스 입력 요소에 사용되며, MVVM 구조를 해치지 않고 View에서 손쉽게 설정 가능합니다. \endif \if EN Encapsulates focus-on-load behavior functionality and related state. \endif
EnterKeyCommandBehavior
\if KO 커맨드를 가져옵니다. \endif \if EN Gets the command value. \endif
obj— \if KO 동작 또는 연결 속성을 적용할 종속성 객체입니다. \endif \if EN The dependency object to which the behavior or attached property applies. \endif반환: \if KO Get Command 작업에서 생성한 결과입니다. \endif \if EN The result produced by the get command operation. \endif
\if KO 커맨드 속성이 변경되면 KeyDown 이벤트를 연결합니다. \endif \if EN Handles the command changed event or state change. \endif
d— \if KO 동작 또는 연결 속성을 적용할 종속성 객체입니다. \endif \if EN The dependency object to which the behavior or attached property applies. \endife— \if KO 이벤트와 관련된 데이터를 포함합니다. \endif \if EN Contains data associated with the event. \endif\if KO Enter 키가 눌릴 경우 Command를 실행합니다. \endif \if EN Handles the key down event or state change. \endif
sender— \if KO 이벤트를 발생시킨 객체입니다. \endif \if EN The object that raised the event. \endife— \if KO 이벤트와 관련된 데이터를 포함합니다. \endif \if EN Contains data associated with the event. \endif\if KO 커맨드를 설정합니다. \endif \if EN Sets the command value. \endif
obj— \if KO 동작 또는 연결 속성을 적용할 종속성 객체입니다. \endif \if EN The dependency object to which the behavior or attached property applies. \endifvalue— \if KO 적용할 값입니다. \endif \if EN The value to apply. \endif\if KO 실행할 커맨드를 지정하는 의존 속성입니다. \endif \if EN Stores the command property value. \endif
FocusOnLoadedBehavior
\if KO Behavior의 속성에서 포커스 활성화 여부를 가져옵니다. \endif \if EN Gets the is enabled value. \endif
obj— \if KO 동작 또는 연결 속성을 적용할 종속성 객체입니다. \endif \if EN The dependency object to which the behavior or attached property applies. \endif반환: \if KO Get Is Enabled 조건이 충족되면 이고, 그렇지 않으면 입니다. \endif \if EN when the get is enabled condition is satisfied; otherwise, . \endif
\if KO 컨트롤이 로드되면 포커스를 자동으로 설정합니다. \endif \if EN Handles the element loaded event or state change. \endif
sender— \if KO 이벤트를 발생시킨 객체입니다. \endif \if EN The object that raised the event. \endife— \if KO 이벤트와 관련된 데이터를 포함합니다. \endif \if EN Contains data associated with the event. \endif\if KO IsEnabled 값이 변경되었을 때의 처리입니다. 로드 완료 시점에 포커스를 설정합니다. \endif \if EN Handles the is enabled changed event or state change. \endif
d— \if KO 동작 또는 연결 속성을 적용할 종속성 객체입니다. \endif \if EN The dependency object to which the behavior or attached property applies. \endife— \if KO 이벤트와 관련된 데이터를 포함합니다. \endif \if EN Contains data associated with the event. \endif\if KO Behavior의 속성에서 포커스 활성화 여부를 설정합니다. \endif \if EN Sets the is enabled value. \endif
obj— \if KO 동작 또는 연결 속성을 적용할 종속성 객체입니다. \endif \if EN The dependency object to which the behavior or attached property applies. \endifvalue— \if KO 적용할 값입니다. \endif \if EN The value to apply. \endif\if KO 포커스 활성화 여부를 나타내는 의존 속성입니다. \endif \if EN Stores the is enabled property value. \endif