C# Caliburn Micro,Wpf绑定嵌套模型
嗨,我有一个应用程序Wpf与Caliburn Micro和MongoDb 我有这样的收藏C# Caliburn Micro,Wpf绑定嵌套模型,c#,.net,mongodb,mvvm,caliburn.micro,C#,.net,Mongodb,Mvvm,Caliburn.micro,嗨,我有一个应用程序Wpf与Caliburn Micro和MongoDb 我有这样的收藏 [Bson IgnoreExtraElements] public class ResourceCollection : CompanyModel { public ResourceCollection(string Vat) : base(Vat) { } private long _ResourceID; public long ResourceID
[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类的集合不会触发,我如何将类绑定到textbox
提前感谢它不启动的原因很简单:资源对象没有设置,您只设置了它的一个属性。要解决此问题,您可以创建一个新的属性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}"
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);
}