2016-10-04 8 views
1

私のViewModelには2つのパブリックプロパティFooBarがあります。 Fooは単なる文字列であり、Barはパブリックプロパティ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> 

のViewModel:

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でバークラスのインスタンスの名前プロパティが満たされている(とバープロパティが正しくインスタンス化されていることを確認しています)?ラベル(または親や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のViewModel:

<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のでは、あなたのViewModelに設定しました

0

Vignesh N.さんのコメントありがとうございました私はこの問題を解決することができました。

実際のコードBarでは変更が可能ですが、最初は名前が空文字列です。これは、ウィンドウが開いたときにラベルに表示されるものです。 Labelは、Barプロパティが変更されたときに通知を受け取らないため、更新されません。

ソリューション:ViewModelにはINotifyPropertyChangedインタフェースを実装し、このようなBarを定義してください:

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

    set 
    { 
     if (_bar != value) 
     { 
      _bar = value; 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Bar))); 
     } 
    } 
} 
関連する問題