2016-10-04 3 views
1

내 ViewModel에 두 개의 공용 속성 FooBar이 있습니다. Foo은 문자열이며 Bar은 문자열 인 public 속성 Name을 가진 클래스입니다.DataBinding에서 속성의 속성에 액세스

GUI 요소에 Bar.Name을 바인딩하고 싶습니다. 어떻게하면됩니까?

<Label Content="{Binding Foo}"> 문자열에 예상대로 문자열 Foo을 씁니다.

그러나 <Label Content="{Binding Bar.Name}">Bar의 이름을 레이블에 쓰지 않습니다. 대신 레이블은 비어 있습니다.

편집 : 내 XAML (따라서 레이블)의 DataContext이 ViewModel로 설정됩니다.

EDIT2 : 물론 실제 코드는 위에서 설명한 것처럼 간단하지 않습니다. 난 단지 위의 설명을 나타내는 최소한의 작업 예를 구축 :

XAML :

<Window x:Class="MyTestNamespace.MyXAML" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"> 
    <StackPanel> 
     <Label Content="{Binding Foo}"></Label> 
     <Label Content="{Binding Bar.Name}"></Label> <!-- Works fine! --> 
    </StackPanel> 
</Window> 

뷰 모델 :

namespace MyTestNamespace 
{ 
    class MyVM 
    { 
     public string Foo { get; set; } 
     public MyBar Bar { get; set; } 

     public MyVM() 
     { 
      Foo = "I am Foo."; 
      Bar = new MyBar("I am Bar's name."); 
     } 
    } 

    class MyBar 
    { 
     public string Name { get; set; } 

     public MyBar(string text) 
     { 
      Name = text; 
     } 
    } 
} 

예상대로이 실제로 작업을 수행합니다. 실제 코드를 (너무 많이 소유하고 회사가 소유 한) 실제 코드를 공유 할 수 없으므로 직접 원인을 검색해야합니다. 가능한 이유에 대한 힌트를 환영합니다!

+0

이 있습니까 당신 있는지 당신의 ViewModel에있는 바 클래스 인스턴스의 이름 속성은 가득 (그리고 바 속성이 올바르게 인스턴스화되고있는)? Label (또는 부모 또는 DataTemplate 중 하나)에 DataContext가 어떻게 설정되어 있습니까? –

+0

예, 모든 것이 올바르게 설정되었습니다. 코드에서'Bar.Name'으로 정상적으로 작업 할 수 있습니다. 'Bar' 나'Bar.Name'도 null이 아닙니다. – Kjara

+0

통찰력을 높이기 위해 코드를 공유하십시오. –

답변

0

있는 1.Your Model.cs :

public class Model : INotifyPropertyChanged 
{ 
    private string _name; 
    public string Name 
    { 
     get { return _name; } 
     set 
     { 
      _name = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("Name")); 
     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged = delegate { }; 
} 

2.Your 뷰 모델 :

<Window x:Class="WpfApplication1.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" 
    mc:Ignorable="d" 
    Title="MainWindow" 
    DataContext="{StaticResource MainViewModel}"> 
<Grid> 
    <Label Content="{Binding Model.Name}"/> 
</Grid> 

:

public MainViewModel() 
    { 
    _model = new Model {Name = "Prop Name" }; 
    } 



    private Model _model; 
    public Model Model 
    { 
     get 
     { 
      return _model; 
     } 
     set 
     { 
      _model = value;  
     } 
    } 

3.Your보기의 DataContext와 함께 당신의 뷰 모델로 설정

0

Vignesh N. 님의 댓글 주셔서 감사합니다. 나는 그 문제를 해결할 수 있었다.

실제 코드 Bar에서 변경할 수 있지만 처음에는 이름이 빈 문자열입니다. 이것은 창을 열었을 때 레이블에 표시되는 것입니다. LabelBar 속성이 변경 될 때 알림을받지 않으므로 업데이트되지 않습니다.

해결 방법 : 뷰 모델이 INotifyPropertyChanged 인터페이스를 구현하고이 같은 Bar 정의합니다 :

private MyBar _bar; 
public MyBar Bar 
{ 
    get 
    { 
     return _bar; 
    } 

    set 
    { 
     if (_bar != value) 
     { 
      _bar = value; 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Bar))); 
     } 
    } 
} 
관련 문제