当前位置: 首页 > news >正文

Avalonia 根据绑定的数据类型动态选择模板

public partial class CommandParameterDialog : Window
{public CommandParameterDialog(){InitializeComponent();// 挂工厂事件var factory = this.FindResource("ParameterTemplateFactory") as RecyclingElementFactory;factory!.SelectTemplateKey += OnSelectTemplateKey;}private void OnSelectTemplateKey(object? sender, SelectTemplateEventArgs e){e.TemplateKey = e.DataContext switch{DateTimeParameterViewModel => "DateTimeTemplate",StringParameterViewModel => "StringTemplate",EnumParameterViewModel => "EnumTemplate",Int32ParameterViewModel => "Int32Template",DoubleParameterViewModel => "DoubleTemplate",_ => throw new ArgumentOutOfRangeException()};}protected override void OnDataContextChanged(EventArgs e){base.OnDataContextChanged(e);if (DataContext is CommandParameterDialogViewModel vm){vm.CloseAction = result =>{Close(result);};}}
}
  <Design.DataContext><viewModels:CommandParameterDialogViewModel></viewModels:CommandParameterDialogViewModel></Design.DataContext><Window.Resources><RecyclingElementFactory x:Key="ParameterTemplateFactory"><RecyclingElementFactory.Templates><!-- 模板:INT32 --><DataTemplate x:Key="Int32Template" DataType="{x:Type viewModels:Int32ParameterViewModel}"><StackPanel Margin="0,8"><TextBlock Text="{Binding DisplayName}" FontWeight="Bold" /><TextBox Text="{Binding Value, Mode=TwoWay}"Watermark="请输入整数" /></StackPanel></DataTemplate><!-- 模板:DOUBLE --><DataTemplate x:Key="DoubleTemplate" DataType="{x:Type viewModels:DoubleParameterViewModel}"><StackPanel Margin="0,8"><TextBlock Text="{Binding DisplayName}" FontWeight="Bold" /><TextBox Text="{Binding Value, Mode=TwoWay}"Watermark="请输入小数" /></StackPanel></DataTemplate><!-- 模板:STRING --><DataTemplate x:Key="StringTemplate" DataType="{x:Type viewModels:StringParameterViewModel}"><StackPanel Margin="0,8"><TextBlock Text="{Binding DisplayName}" FontWeight="Bold" /><TextBox Text="{Binding Value, Mode=TwoWay}"Watermark="请输入文本" /></StackPanel></DataTemplate><!-- 模板:ENUM --><DataTemplate x:Key="EnumTemplate" DataType="{x:Type viewModels:EnumParameterViewModel}"><StackPanel Margin="0,8"><TextBlock Text="{Binding DisplayName}" FontWeight="Bold" /><ComboBox ItemsSource="{Binding Options}"SelectedItem="{Binding SelectedValue, Mode=TwoWay}" PlaceholderText="请选择" /></StackPanel></DataTemplate><!-- 模板:DATETIME --><DataTemplate x:Key="DateTimeTemplate" DataType="{x:Type viewModels:DateTimeParameterViewModel}"><StackPanel Margin="0,8"><TextBlock Text="{Binding DisplayName}" FontWeight="Bold" /><DatePicker SelectedDate="{Binding Value, Mode=TwoWay}" /></StackPanel></DataTemplate></RecyclingElementFactory.Templates></RecyclingElementFactory></Window.Resources><DockPanel Margin="16"><!-- 底部按钮 --><StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right" Spacing="8"><Button Content="取消" Width="80" Command="{Binding CancelCommand}" /><Button Content="确定" Width="80" Command="{Binding ConfirmCommand}"IsEnabled="{Binding IsValid}" /></StackPanel><!-- 参数列表 --><ScrollViewer HorizontalScrollBarVisibility="Disabled"VerticalScrollBarVisibility="Auto"><ItemsRepeater ItemsSource="{Binding Parameters}" ItemTemplate="{StaticResource ParameterTemplateFactory}"><ItemsRepeater.Layout><StackLayout Orientation="Vertical" Spacing="8" /></ItemsRepeater.Layout></ItemsRepeater></ScrollViewer></DockPanel>
http://www.hskmm.com/?act=detail&tid=13111

相关文章:

  • PyTorch图神经网络(一)
  • Python版Sigstore稳定版发布:软件供应链签名新标准
  • 网速带宽概念
  • 跨网传输软件:打通数据孤岛,保障安全流通!
  • 「KDOI-07」能量场
  • 基于LQR控制器的柔性机械臂抑振
  • 202507_QQ_caidundun
  • 国内企业邓白氏编码免费申请流程
  • 在CodeBolcks下wxSmith的C++编程教程——wxSmith教程目录(序言)
  • 生命周期
  • CF1893D Colorful Constructive 题解
  • C#通过15位或者18位身份证判断性别年龄
  • 深入解析:​​XMedia Recode 全能视频音频转换与编辑工具
  • MySQL同步ES的 5 种方案
  • 如何支持高并发高吞吐量编程
  • outlook大附件发送是什么?
  • 好用的提示词
  • 202312_Dest0g3_StrageTraiffic
  • 2025年内外网文件传输新范式:十大好用的内外网文件摆渡系统
  • 双分布函数热 LBM 模拟二维封闭方腔自然对流
  • asp.net中的wwwroot是什么
  • 用光学计算加速AI模型中的卷积和矩阵乘法操作
  • 了解IWebHostEnvironment : IHostEnvironment
  • PDF24 Creator(完全免费多功能PDF工具箱) 易于使用 多语言支持 - 教程
  • 彩笔运维勇闯机器学习--lasso回归
  • IP地址的配置
  • 【2025-09-21】连岳摘抄
  • 矩阵、线性代数 - 指南
  • 【2025-09-20】经营套路
  • 基于 uni-app 开发的废品回收类多端应用功能与界面说明