【问题标题】:Conditional DataTemplate Selection using bindings Avalonia使用绑定 Avalonia 的条件 DataTemplate 选择
【发布时间】:2021-02-11 10:36:28
【问题描述】:

我是 Avalonia 的新手,我需要为我的一个项目生成问题和答案列表。到目前为止,我已经根据需要生成了问题和答案。 XAML 代码

                <ItemsControl Items="{Binding Questions}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock
                                Classes="header"
                                Text="{Binding QuestionDescription}"
                                TextWrapping="Wrap" />
                            <ItemsControl Items="{Binding Answers}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <CheckBox x:Name="{Binding AId}" Content="{Binding Answer}" />
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

我现在想要的是根据我从Questions 列表中获得的AnswerType 值创建不同类型的回答选项(单选按钮或复选框)。这是我的问题模型

public class Question
{
    public string QId { get; set; }
    public string QuestionDescription { get; set; }
    public List<Answers> Answers { get; set; }
    public string AnswerType { get; set; }
}

public class Answers
{
    public string AId { get; set; }
    public string Answer { get; set; }
}

样本数据

  {
    "QId": "Q1",
    "QuectionDescription": "Quection01",
    "Answers": [
      {
        "AId": "Q1A1",
        "Answer": "Yes"
      },
      {
        "AId": "Q1A2",
        "Answer": "No"
      }
    ],
    "AnswerType": "RadioButton"
  },
  {
    "QId": "Q2",
    "QuectionDescription": "Quection02",
    "Answers": [
      {
        "AId": "Q2A1",
        "Answer": "Football"
      },
      {
        "AId": "Q2A2",
        "Answer": "Baseball"
      }
    ],
    "AnswerType": "CheckBox"
  }

【问题讨论】:

    标签: wpf xaml data-binding avaloniaui avalonia


    【解决方案1】:
    
    public class TemplateDictionaryConverter : Dictionary<string, IDataTemplate>, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string s)
                return this[s];
            return null;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
            => throw new NotSupportedException();
    }
    
    <ItemsControl Items="{Binding Answers}">
        <ItemsControl.ItemTemplate>
            <Binding
                Path="AnswerType">
                <Binding.Converter>
                    <example:TemplateDictionaryConverter>
                        <DataTemplate x:Key="CheckBox">
                            <CheckBox Content="{Binding Answer}" />
                        </DataTemplate>
                        <DataTemplate x:Key="RadioButton">
                            <RadioButton Content="{Binding Answer}" />
                        </DataTemplate>
                    </example:TemplateDictionaryConverter>
                </Binding.Converter>
            </Binding>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    【讨论】:

    • 这是我所期待的。非常感谢! @kekekeks
    猜你喜欢
    • 2020-11-22
    • 2021-07-03
    • 2014-06-06
    • 2012-11-01
    • 2022-10-24
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 2016-03-21
    相关资源
    最近更新 更多