数据模板 + MVVM

发布于 2024-08-17 12:07:13 字数 300 浏览 4 评论 0原文

我正在使用 MVVM,每个视图都映射到具有约定的 ViewModel。 IE MyApp.Views.MainWindowView MyApp.ViewModels.MainWindowViewModel

有没有办法删除 DataTemplate 并在 C# 中执行它?有某种循环?

<DataTemplate DataType="{x:Type vm:MainWindowViewModel}">
    <vw:MainWindowView />
</DataTemplate>

I'm using MVVM and each View maps to a ViewModel with a convention. IE
MyApp.Views.MainWindowView
MyApp.ViewModels.MainWindowViewModel

Is there a way to remove the DataTemplate and do it in C#? with some sort of loop?

<DataTemplate DataType="{x:Type vm:MainWindowViewModel}">
    <vw:MainWindowView />
</DataTemplate>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

書生途 2024-08-24 12:07:13

所以基本上,您需要以编程方式创建数据模板...这不是很简单,但我认为您可以使用 FrameworkElementFactory 类来实现:

public void AddDataTemplateForView(Type viewType)
{
    string viewModelTypeName = viewType.FullName + "Model";
    Type viewModelType = Assembly.GetExecutingAssembly().GetType(viewModelTypeName);

    DataTemplate template = new DataTemplate
    {
        DataType = viewModelType,
        VisualTree = new FrameworkElementFactory(viewType)
    };

    this.Resources.Add(viewModelType, template);
}

我没有测试它,所以可能需要进行一些调整必要...例如,我不确定资源键的类型应该是什么,因为它通常是在 XAML 中设置 DataType 时隐式设置的

So basically, you need to create data templates programmatically... That's not very straightforward, but I think you can achieve that with the FrameworkElementFactory class :

public void AddDataTemplateForView(Type viewType)
{
    string viewModelTypeName = viewType.FullName + "Model";
    Type viewModelType = Assembly.GetExecutingAssembly().GetType(viewModelTypeName);

    DataTemplate template = new DataTemplate
    {
        DataType = viewModelType,
        VisualTree = new FrameworkElementFactory(viewType)
    };

    this.Resources.Add(viewModelType, template);
}

I didn't test it, so a few adjustments might be necessary... For instance I'm not sure what the type of the resource key should be, since it is usually set implicitly when you set the DataType in XAML

葮薆情 2024-08-24 12:07:13

谢谢托马斯,使用你的代码我已经做到了这一点。

添加资源时需要使用 DataTemplateKey :D

    private void AddAllResources()
    {
        Type[] viewModelTypes = Assembly.GetAssembly(typeof(MainWindowViewModel)).GetTypes()
            .Where(t => t.Namespace == "MyApp.ViewModels" && t.Name.EndsWith("ViewModel")).ToArray();

        string viewName = null;
        string viewFullName = null;

        foreach (var vmt in viewModelTypes)
        {
            viewName = vmt.Name.Replace("ViewModel", "View");
            viewFullName = String.Format("MyApp.Views.{0}, MyApp", viewName);

            DataTemplate template = new DataTemplate
            {
                DataType = vmt,
                VisualTree = new FrameworkElementFactory(Type.GetType(viewFullName, true))
            };

            this.Resources.Add(new DataTemplateKey(vmt), template);
        }
    }

Thanks Thomas, using your code i've done this.

You need to use the DataTemplateKey when adding the resoures :D

    private void AddAllResources()
    {
        Type[] viewModelTypes = Assembly.GetAssembly(typeof(MainWindowViewModel)).GetTypes()
            .Where(t => t.Namespace == "MyApp.ViewModels" && t.Name.EndsWith("ViewModel")).ToArray();

        string viewName = null;
        string viewFullName = null;

        foreach (var vmt in viewModelTypes)
        {
            viewName = vmt.Name.Replace("ViewModel", "View");
            viewFullName = String.Format("MyApp.Views.{0}, MyApp", viewName);

            DataTemplate template = new DataTemplate
            {
                DataType = vmt,
                VisualTree = new FrameworkElementFactory(Type.GetType(viewFullName, true))
            };

            this.Resources.Add(new DataTemplateKey(vmt), template);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文