c# - Catel ViewModelToModel 没有链接

标签 c# .net wpf catel

我有一个简单的 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/

相关文章:

c# - WPF MVVM : how to bind GridViewColumn to ViewModel-Collection?

c# - 解决类型是否支持接口(interface)的最佳方法? (鸭子类型(duck typing))

c# - 使用 MStest 和最小起订量测试 TryUpdateModel()

C# 在范围外重新抛出异常

c# - 我在 sqlConnection 中发现的那些奇怪的属性是什么?

c# - “Long”延迟导致我的视​​图的按钮无法重新启用

C# Winforms | C#表格边框厚度

javascript - 如何从 C# 绑定(bind)到 HTML?

.net - 没有端点监听 net.pipe ://localhost/

wpf - slider 不适用于触摸输入