我有一个简单的 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]
-Attribute。
现在我想将 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
,尽管模型本身已正确初始化。
我是不是遗漏了什么或者这根本不可能?
提前致谢!
最佳答案
尝试在 ViewModelToModel 属性上设置 MappingType
,以便模型获胜。
关于c# - Catel ViewModelToModel 没有链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44386571/