ClemensGerstung
ClemensGerstung

Reputation: 141

Catel ViewModelToModel not linking

I have a simple ViewModel which has two Models. So it looks like this:

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 extends ModelBase so I can use the [Model]-Attribute several times.

Now I want to expose the properties of the Model to the 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
    ...
}

The problem is that the linking doesn't work. So after initialization the property AvailableConnections (and the others also) are still null, though the Model itself is initialized correctly.

Am I missing something or isn't this possible at all?

thx in advance!

Upvotes: 2

Views: 258

Answers (1)

Geert van Horrik
Geert van Horrik

Reputation: 5724

Try setting the MappingType on the ViewModelToModel attribute so that the model wins.

Upvotes: 1

Related Questions