【问题标题】:Caliburn Micro, Wpf binding nested modelCaliburn Micro,Wpf 绑定嵌套模型
【发布时间】:2018-07-06 20:28:00
【问题描述】:

您好,我有一个带有 Caliburn Micro 和 MongoDb 的应用程序 Wpf 我有一个这样的收藏

[Bson IgnoreExtraElements]
public class ResourceCollection : CompanyModel
{
    public ResourceCollection(string Vat) : base(Vat)
    {
    }

    private long _ResourceID;

    public long ResourceID
    {
        get { return _ResourceID; }
        set { _ResourceID = value; }
    }

    private string _Description;

    public string Description
    {
        get { return _Description; }
        set
        {
            _Description = value;
            NotifyOfPropertyChange(() => Description);
        }
    }
}

CompanyModel 继承自 PropertyChangedBase,并且我有一个视图模型:

public class ResourceCreateViewModel : Screen
{
    private IWindowManager _windowManager;
    private readonly AppConnection _appConnection;
    private readonly ResourceRepository _resourceRepository;

    private ResourceCollection _Resource;
    public ResourceCollection Resource
    {
        get
        {
            return _Resource;
        }
        set
        {
            _Resource = value;
            NotifyOfPropertyChange(() => Resource);
            NotifyOfPropertyChange(() => CanSave);
        }
    }
}

这是我的 xaml

  <TextBox Text="{Binding Resource.Description, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="20" DockPanel.Dock="Right"></TextBox>

我的问题是,当我更改 texbox 内的值时,我的 viewmodel 类的集合没有触发,我如何将我的类绑定到文本框?

提前谢谢你

【问题讨论】:

    标签: c# .net mongodb mvvm caliburn.micro


    【解决方案1】:

    它不触发它的原因很简单:Resource 对象没有被设置,你只是在它上面设置了一个属性。要解决此问题,您可以创建一个新属性 ResourceDescription 并绑定到该属性:

        public string ResourceDescription
        {
        get
        {
            return _Resource.Description;
        }
                set
                {
                    _Resource.Description = value;
                    NotifyOfPropertyChange(() => ResourceDescription);
                    NotifyOfPropertyChange(() => Resource);
                    NotifyOfPropertyChange(() => CanSave);
                }
        }
    

    Xaml:

    <TextBox Text="{Binding Resource.Description, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
    

    但这有其自身的问题,因为视图模型中的更改不再更新您的视图。相反,您可以订阅资源 PropertyChanged 事件:

    private ResourceCollection _Resource;
    public ResourceCollection Resource
    {
        get
        {
            return _Resource;
        }
        set
        {
            if(_Resource != null)
            {
                _Resource.PropertyChanged -= ResourcePropertyChanged;
            }
    
            _Resource = value;
    
            if(_Resource != null)
            {
                _Resource.PropertyChanged += ResourcePropertyChanged;
            }
    
            NotifyOfPropertyChange(() => Resource);
            NotifyOfPropertyChange(() => CanSave);
        }
    }
    
    private void ResourcePropertyChanged(object sender, EventArgs e)
    {
        //you might be able to do something better than just notify of changes here
        NotifyOfPropertyChange(() => Resource);
        NotifyOfPropertyChange(() => CanSave);
    }
    

    这会很快变得复杂,尤其是当您订阅嵌套在对象图中更深的属性时。

    【讨论】:

      猜你喜欢
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      相关资源
      最近更新 更多