绑定到ViewModel的子类属性 [英] Binding to a ViewModel's Child Class Properties

查看:175
本文介绍了绑定到ViewModel的子类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将我视图中的StatusBarItem内容元素绑定到ViewModel中的子类属性,我正在使用MVVM-Light框架/

I'm struggling to be able to bind a StatusBarItem content element in my view to a subclasses property in my ViewModel, I'm using the MVVM-Light framework/

ViewModel:

ViewModel:

public class PageMainViewModel : ViewModelBase
{
    LoggedOnUserInfo UserInfo;

    public LoggedOnUser UserInfo
    {
       set
       {
          _UserInfo = value;
          RaisePropertyChanged("UserInfo");
       }
    }
}

为了完整清楚LoggedOnUser类定义如下

For full clarity the LoggedOnUser Class is defined as follows

public class LoggedOnUser : INotifyPropertyChanged
{
    private string _Initials;        

    public event PropertyChangedEventHandler PropertyChanged;            

    public LoggedOnUser()
    {

    }

    [DataMember]
    public string Initials
    {
        get { return _Initials; }
        set 
        { 
             _Initials = value;
            OnPropertyChanged("Initials");
        }
    }

    protected void OnPropertyChanged(string propValue)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propValue));
        }
    }

}

我的意见DataContext正在设置,并且正在工作,因为我可以看到其他绑定工作,但我尝试绑定到我的XAML中的UserInfo.Initials属性正在产生一个空的结果。

My Views DataContext is being set and is working as I am able to see other bindings working, but my attempts to bind to UserInfo.Initials property in my XAML are producing an empty result.

XAML:

<StatusBarItem Grid.Column="0" Content="{Binding UserInfo.Initials}" Margin="5,0,0,0" VerticalAlignment="Center" Focusable="False" />

UserInfo属性是在viewModel创建之后设置的,因为有几个因素,但我认为我的propertychanged事件这将是可以的。

The UserInfo property is set after the viewModel is created due to several factors but I thought with my propertychanged events this would be ok.

对此的任何建议将不胜感激。

Any Advice on this would be greatly appreciated.

推荐答案

UserInfo 中似乎没有吸引者,绑定将不幸。

You do not appear to have a getter on UserInfo, the binding will be out of luck.

(另外检查绑定错误当遇到麻烦的绑定,他们可能会告诉你所有他们的问题)

(Also check for binding errors when having trouble with bindings, they probably will tell you about all their problems)

这篇关于绑定到ViewModel的子类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆