2013-04-17 2 views
0

의 DependencyProperty에 아이에 바인딩 할 수 없습니다는 내 이미지를 바인딩하는 방법을 알아낼 수 없습니다 DependencyObject에

: DependencyObject에서 파생 된 내 사용자 정의 클래스의 하위 요소입니다 DependencyProperty에 ProcessedImage

<Image Source="{ Binding Path=ViewModel.MainViewModel.ProcessedImage }" Name="image1"/> 

class ViewModel : DependencyObject 
{ 
    public static ViewModel MainViewModel { get; set; } 

    [...] 

    public BitmapImage ProcessedImage 
    { 
     get { return (BitmapImage)this.GetValue(ProcessedImageProperty); } 
     set { this.SetValue(ProcessedImageProperty, value); } 
    } 
    public static readonly DependencyProperty ProcessedImageProperty = DependencyProperty.Register(
     "ProcessedImage", typeof(BitmapImage), typeof(ViewModel), new PropertyMetadata()); 
} 

나는 당신이 나를 도울 수 있기를 바랍니다. 나는 다른 접근법을 시도했지만 아무것도 작동하지 않는 것 같습니다.

+0

VM이 DependencyObject를 기반으로하는 이유는 무엇입니까? 아마도 INotifyPropertyChanged를 사용하고 뷰의 DataContext를 뷰 모델과 동일하게 설정해야합니다. 그런 다음 POCO가 될 수있는 ProcessedImage 경로를 설정할 수 있습니다. –

+0

나는이 방법을 선택했는데, 이는 나에게 더 단순 해 보였기 때문입니다. 나는 그것이 가능하다면 오히려 그것을 이렇게 유지할 것이다. DependencyObject 클래스의 자식 요소에 바인딩 할 수 있습니까? – wpfnoop

+0

무언가가 더 간단하기 때문에, 반드시 옳다는 것을 의미하지는 않습니다. MVVM 패턴은 나중에 더 쉽게 유지 보수를 작성하기위한 것입니다. UI에 대한 종속성을 만들려면 WinForms 코딩을 사용하십시오 (WPF를 사용하여 코딩 할 수 있습니다). –

답변

1

어떻게 데이터 컨텍스트를 설정하고 있습니까? 난 당신의 코드를 복사하여 다른 속성을 추가 - ProcessedImageName을 다음과 같이 나는 데이터 컨텍스트를 설정

public static readonly DependencyProperty ProcessedImageNameProperty = DependencyProperty.Register(
     "ProcessedImageName", typeof(string), typeof(ViewModel), new PropertyMetadata("Hello World")); 

    public string ProcessedImageName { 
get { return (string)this.GetValue(ProcessedImageNameProperty); } 
set { this.SetValue(ProcessedImageNameProperty, value); }} 

은 "안녕하세요"의 기본값 : 나는대로 바인딩 경로를 설정

public MainWindow() 
    { 
     InitializeComponent(); 
     ViewModel.MainViewModel = new ViewModel(); 
     DataContext = ViewModel.MainViewModel; 
    } 

를 :

<TextBlock Text="{Binding Path=ProcessedImageName }"/> 

개인적으로, 나는 정적 MainViewModel 속성을 계속할 것 등

같은 것은 최대 대신 새로 만들기 뷰 모델 인스턴스
public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = new ViewModel(); 
    } 

속성에 대한 경로는 데이터 컨텍스트와 관련되어 있으므로 속성이 Class.PropertyName이고 데이터 컨텍스트가 클래스 인 경우 바인딩 경로는 PropertyName입니다.

+0

대단히 감사합니다. – wpfnoop

관련 문제