WPF

디자인 타임에만 사용되는 ViewModel 바인딩

NicSub 2025. 7. 31. 10:18
728x90
반응형

d:DataContext는 WPF에서 디자인 타임에만 사용되는 ViewModel 바인딩입니다. 주로 Visual Studio의 XAML 디자이너 미리보기에서 실제 데이터 바인딩 결과를 확인하고 싶을 때 사용합니다.

 

<Window x:Class="YourApp.MainWindow"
        xmlns:local="clr-namespace:YourApp.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DataContext="{d:DesignInstance Type=local:MainViewModel, IsDesignTimeCreatable=True}">
    
    <TextBlock Text="{Binding Title}" />
</Window>

 

🧠 왜 디자인 타임 미리보기가 중요할까?

  1. UI 레이아웃 조정이 쉬워집니다.
  2. → 빈 화면이 아니라 실제 데이터로 레이아웃을 잡을 수 있어서 더 효율적입니다.
  3. 디자인 도중 바인딩 경로 오류를 빨리 찾을 수 있습니다.
  4. → 예: Text="{Binding WrongProperty}" 같은 오타
  5. Blend를 사용할 때 필수
  6. → Blend for Visual Studio에서 MVVM 기반 앱 디자인을 할 때 필수적으로 활용됩니다.

⚠️ 주의사항

  • 런타임에 전혀 영향을 주지 않습니다.
  • (즉, d:DataContext로 바인딩한 ViewModel은 실제 실행 시 적용되지 않음)
  • ViewModel에 파라미터 없는 생성자가 있어야 합니다.
  •  IsDesignTimeCreatable=True일 때 자동 인스턴스 생성이 가능해야 하므로.
반응형