Catel ViewModelToModel 没有链接
Catel ViewModelToModel not linking
我有一个简单的 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。
现在我想向 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
,以便模型获胜。
我有一个简单的 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。
现在我想向 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
,以便模型获胜。