Ich habe eine einfache ViewModel
, die zwei Models
hat. So sieht es wie folgt aus:Catel ViewModelToModel nicht verknüpfen
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
erstreckt ModelBase
so kann ich die [Model]
-Attribut mehrmals verwenden.
Jetzt mag ich die Eigenschaften des Modells auf das Ansichtsmodell belichten:
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
...
}
Das Problem besteht darin, dass die Verbindung nicht funktioniert. Nach der Initialisierung ist die Eigenschaft AvailableConnections
(und auch die anderen) immer noch null
, obwohl das Modell selbst korrekt initialisiert wird.
Fehle ich etwas oder ist das überhaupt nicht möglich?
Danke im Voraus!
Danke! Die Einstellung 'Mode = ViewModelToModelMode.Explicit' funktioniert! – HumpaLumpa007