根据Avalonia中的DataContext属性选择一个DataTemplate [英] Selecting a DataTemplate based on DataContext property in Avalonia

查看:117
本文介绍了根据Avalonia中的DataContext属性选择一个DataTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个UserControl,该控件应显示设置列表:

I'm implementing a UserControl which should display a list of settings:

public class SettingPropertyItem {
    string Name { get; }
    Type ValueType { get; }
    object Value { get; set; }
}

基于 ValueType 中的每种类型,应使用不同的DataTemplate.
为方便起见,UserControl具有以下控件,该控件以 SettingPropertyItem 作为其DataContext:

Based on each type in ValueType a different DataTemplate should be used.
To facilitate this the UserControl has the following Control with a SettingPropertyItem as its DataContext:

<UserControl x:Class="AVDump3Gui.Controls.Settings.SettingsView">
    ...
    <ItemsControl Items="{Binding Properties}" Margin="16,0,0,0">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
            ...
                <ContentControl Content="{Binding}"/>
            ...
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    ...
</UserControl>

然后在使用Usercontrol的视图中,我已经在其DataTemplates中添加了一个DataTemplate:

Then the view where the Usercontrol is used, I've added a DataTemplate into its DataTemplates:

<sv:SettingsView.DataTemplates>
  <DataTemplate DataType="{x:Type vm:SettingPropertyItem}">
    ...
  </DataTemplate>
</sv:SettingsView.DataTemplates>

到目前为止,一切正常.但是现在我有些困惑,因为我不知道如何基于DataContext中的属性来应用不同的DataTemplates.
使用WPF,似乎可以使用DataTemplateSelector或Triggers(忽略其他框架),但在Avalonia中似乎并不存在.我也尝试过样式,但是选择器似乎无法访问DataContext属性.

So far so good, everything works as expected. But now I'm a bit stumped as I don't know how to apply different DataTemplates based on a Property within the DataContext.
With WPF a DataTemplateSelector or Triggers seem to be the way to go (ignoring additional Frameworks), but they don't seem to exist in Avalonia. I've also tried styles but the selector doesn't seem to be able to access DataContext properties.

这怎么办?

推荐答案

在Avalonia中不需要 DataTemplateSelector ,因为您可以自己实现 IDataTemplate 并在其中选择模板

In Avalonia DataTemplateSelector isn't needed because you can just implement IDataTemplate yourself and select the template there.

i.e.

public class MyTemplateSelector : IDataTemplate
{
    public bool SupportsRecycling => false;
    [Content]
    public Dictionary<string, IDataTemplate> Templates {get;} = new Dictionary<string, IDataTemplate>();

    public IControl Build(object data)
    {
        return Templates[((MyModel) data).Value].Build(data);
    }

    public bool Match(object data)
    {
        return data is MyModel;
    }
}

public class MyModel
{
    public string Value { get; set; }
}

  <ItemsControl>
    <ItemsControl.Items>
      <scg:List x:TypeArguments="local:MyModel">
        <local:MyModel Value="MyKey"/>
        <local:MyModel Value="MyKey2"/>
      </scg:List>
    </ItemsControl.Items>
    <ItemsControl.DataTemplates>
      <local:MyTemplateSelector>
        <DataTemplate x:Key="MyKey">
          <TextBlock Background="Red" Text="{Binding Value}"/>
        </DataTemplate>
        <DataTemplate x:Key="MyKey2">
          <TextBlock Background="Blue" Text="{Binding Value}"/>
        </DataTemplate>
        
      </local:MyTemplateSelector>
    </ItemsControl.DataTemplates>
  </ItemsControl>

这篇关于根据Avalonia中的DataContext属性选择一个DataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆