2017-06-06 27 views
2

我有一個簡單的ViewModel其中有兩個Models。所以它看起來像這樣:Catel ViewModelToModel沒有鏈接

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幾次。

現在我要揭露的模型視圖模型的屬性:

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,儘管模型本身已正確初始化。

我錯過了什麼,或者根本不可能這麼做嗎?

thx提前!

回答

1

嘗試在ViewModelToModel屬性上設置MappingType,以便模型獲勝。

+1

謝謝!設置'Mode = ViewModelToModelMode.Explicit'可以工作! – HumpaLumpa007