根据 Avalonia ItemsControl 中的 ViewModel 类型选择 DataTemplate

如何解决根据 Avalonia ItemsControl 中的 ViewModel 类型选择 DataTemplate

UserControl里面有ItemsControl,需要根据Items集合元素类型显示不同DataTemplate的items。

以下基于此答案创建的 TemplateSelector 和 XAML (Selecting a DataTemplate based on DataContext property in Avalonia)

    <ItemsControl Items="{Binding Items}">
        <ItemsControl.DataTemplates>
            <views:ItemstemplateSelector>
                <DataTemplate x:Key="{x:Type itemviewmodels:Item1viewmodel}">
                    <itemsViews:Item1View/>
                </DataTemplate>
                <DataTemplate x:Key="{x:Type itemviewmodels:Item2viewmodel}">
                    <itemsViews:Item2View/>
                </DataTemplate>
            </views:ItemstemplateSelector>
        </ItemsControl.DataTemplates>
    </ItemsControl>
        public Itemsviewmodel()
        {
            this.Items = new ObservableCollection<IItemviewmodel>();
            this.Items.Add(new Item1viewmodel("Item1"));
            this.Items.Add(new Item2viewmodel("Item2"));
        }

        public ObservableCollection<Iitemviewmodel> Items { get; }
    public class ItemstemplateSelector : IDataTemplate
    {
        public bool SupportsRecycling => false;

        [Content]
        public Dictionary<Type,IDataTemplate> Templates { get; } = new Dictionary<Type,IDataTemplate>();

        public IControl Build(object data)
        {
            var type = data.GetType();
            var template = this.Templates[type];
            var control = template.Build(data);

            return control;
        }

        public bool Match(object data)
        {
            return data is IItemviewmodel;
        }
    }
    public interface IItemviewmodel
    {
        string Name { get; }
    }
    public class Item1viewmodel : IItemviewmodel
    {
        public Item1viewmodel (string name)
        {
            this.Name = name;
        }

        public string Name { get; }
    }
<UserControl
  xmlns="https://github.com/avaloniaui"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  x:Class="Desktop.Views.Items.Item1View">

  <TextBlock Text="{Binding Name}"/>

</UserControl>

在运行时出现异常:

Avalonia.Markup.Xaml.XamlLoadException: '没有为 Desktop.Views.Items.Item1View 找到预编译的 XAML,请确保指定 x:Class 并将您的 XAML 文件包含为 AvaloniaResource'

如果在 ItemsControl 而不是 <itemsViews:Item1View/> 中指定 <TextBlock Text="{Binding Name}"/>,则一切正常。但我想将每个项目的标记拆分为具有单独 viewmodel 的单独 XAML 文件。项目可以完全不同。

能否解决这个问题,让ItemsControl根据viewmodel类型选择DataTemplate?

解决方法

为了解决这个问题,我手动编辑了项目文件 *.csproj 添加了 AvaloniaResource 元素

<AvaloniaResource Include="..\Desktop\Views\Items*.xaml">
    <SubType>Designer</SubType>
</AvaloniaResource>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?