c# - 具有相同 xaml 文件和不同 DataContext 的多个输入表单

标签 c# xaml windows-runtime winrt-xaml mvvm-light

我正在使用 XAML 和 MVVM Light 开发 WinRT 应用程序。这个应用程序旨在让用户在外地时更容易地收集数据。我的应用程序有一个部分,用户需要在其中输入有关几个不同项目的一堆信息。这些项目被定义为继承自 GenericAsset 类的类。 GenericAsset 具有如下字段:

public class GenericAsset
{
    public string AssetId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
}

子类看起来像这样:

public class SubAsset1 : GenericAsset
{
    public string RecordNumber { get; set; }
    public int SizeDiameter { get; set; }
    public string MaterialType { get; set; }
}

public class SubAsset2 : GenericAsset
{
    public string Type { get; set; }
    public int Size { get; set; }
    public string PlanRef { get; set; }
    public string InteriorMaterial { get; set; }
}

目前我有 15 个子 Assets (将来会有更多),我正在寻找一种方法来创建一个数据输入 View / View 模型(如果可能的话),这样我就不必为每项 Assets 。此外,如果我能让通用 View / View 模型正常工作,我将如何加载自定义数据输入控件(特定于每个子 Assets 的输入),同时保持适当的双向数据绑定(bind)到适当的子 Assets ?

最佳答案

您正在寻找的是 DataTemplateSelector。为每个 SubAsset 创建一个不同的 DataTemplate。然后,通过 ContentControl(或 ListView,如果有多个)显示 Asset。它们都有一个用于 DataTemplateSelector 的插槽(分别为 ContentTemplateSelectorItemTemplateSelector)。如果它们之间有相似的部分,您实际上可以通过使用指向目标 DataTemplate 的内部 ContentControl 将一个 DataTemplate 与另一个组合(你想创作的)。

为了添加来自不同ResourceDictionariesDataTemplate,当您创建DataTemplateSelector 时,为每个DataTemplate 创建一个属性 你希望拥有的。

您的选择器可能看起来像这样:

public class AssetDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate SubAsset1DataTemplate { get; set; }
    public DataTemplate SubAsset2DataTemplate { get; set; }
    ...

    // Data Template Selection Code
    ...
}

然后在 ResourceDictionary(例如 Generic.xaml)中,当您声明 AssetDataTemplateSelector 时,只需将所有其他 DataTemplates 引用为 StaticResources.

<!-- This assumes that AssetDataTemplateSelector has been declared in a namespace 
     defined in the root of the ResourceDictionary as 'converters'. -->
<!-- It also assumes that you have created DataTemplates with the names 
     SubAssetXDataTemplate either in the same or other ResourceDictionaries 
     which are accessible from this one. -->
<converters:AssetDataTemplateSelector x:Key="AssetDataTemplateSelector"
                                      SubAsset1DataTemplate="{StaticResource SubAsset1DataTemplate}"
                                      SubAsset2DataTemplate="{StaticResource SubAsset2DataTemplate}"
                                      ...
                                      />

关于c# - 具有相同 xaml 文件和不同 DataContext 的多个输入表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24121205/

相关文章:

c# - 使用配置类或参数实例化对象

wpf - 为什么 Expression Blend 不渲染我的用户控件?它只显示 XAML

.net - Windows 运行时 : Does it include a logging library?

windows-8 - 用 HTML/CSS/JS 编写的 Metro 应用与 XAML/C# 编写的 Metro 应用之间的区别

windows-8 - 如何从 Windows 8 中的 MediaCapture API 获取反馈

c# - DataGridView:在单击或击键时禁用单元格编辑,仅在双击时启用单元格编辑

c# - 在 C# 中通过 HttpWebRequest 实现 Digest 身份验证

c# - 在 Medium Trust 中编译代码

xaml - 如何正确地将功能区库类别绑定(bind)到集合

c# - 如何使用用户控件后台代码中存在的事件处理程序以编程方式打开和关闭 bottomappbar?