Catel ViewModelToModel 没有链接
Posted
技术标签:
【中文标题】Catel ViewModelToModel 没有链接【英文标题】:Catel ViewModelToModel not linking 【发布时间】:2017-06-06 09:42:08 【问题描述】:我有一个简单的ViewModel
,它有两个Models
。所以它看起来像这样:
public class ConnectionItemSelectorViewModel : ViewModelBase
...
#region AvailableConnectionsModel
// Model Nr. 1
[Model]
public ConnectionList AvailableConnectionsModel
get return GetValue<ConnectionList>(AvailableConnectionsModelProperty);
set SetValue(AvailableConnectionsModelProperty, value);
public static readonly PropertyData AvailableConnectionsModelProperty = RegisterProperty(nameof(AvailableConnectionsModel), typeof(ConnectionList), () => new ConnectionList());
#endregion
#region SelectedConnectionsModel
// Model Nr. 2
[Model]
public ConnectionList SelectedConnectionsModel
get return GetValue<ConnectionList>(SelectedConnectionsModelProperty);
set SetValue(SelectedConnectionsModelProperty, value);
public static readonly PropertyData SelectedConnectionsModelProperty = RegisterProperty(nameof(SelectedConnectionsModel), typeof(ConnectionList), () => new ConnectionList());
#endregion
...
ConnectionList
扩展了ModelBase
,所以我可以多次使用[Model]
-属性。
现在我想将 Model 的属性暴露给 ViewModel:
public class ConnectionItemSelectorViewModel : ViewModelBase
...
// init Model properties
#region AvailableConnections
// Using a unique name for the property in the ViewModel
// but linking to the "correct" property in the Model by its name
[ViewModelToModel(nameof(AvailableConnectionsModel), nameof(ConnectionList.Connections))]
public ObservableCollection<ConnectionItem> AvailableConnections
get return GetValue<ObservableCollection<ConnectionItem>>(AvailableConnectionsProperty);
set SetValue(AvailableConnectionsProperty, value);
public static readonly PropertyData AvailableConnectionsProperty = RegisterProperty(nameof(AvailableConnections), typeof(ObservableCollection<ConnectionItem>), () => null);
#endregion
// linking other properties to the models
...
问题是链接不起作用。所以在初始化之后,属性AvailableConnections
(以及其他属性)仍然是null
,尽管模型本身已正确初始化。
是我遗漏了什么还是根本不可能?
提前谢谢!
【问题讨论】:
【参考方案1】:尝试在 ViewModelToModel 属性上设置MappingType
,以便模型获胜。
【讨论】:
谢谢!设置Mode = ViewModelToModelMode.Explicit
有效!以上是关于Catel ViewModelToModel 没有链接的主要内容,如果未能解决你的问题,请参考以下文章
从 Catel WPF UserControl 中的 ResourceDictionary 中绑定