当前位置: 首页 > 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.rkmt.cn/news/9422.html

相关文章:

  • PyTorch图神经网络(一)
  • Python版Sigstore稳定版发布:软件供应链签名新标准
  • 仿照豆包实现 Prompt 变量模板输入框
  • 网速带宽概念
  • 跨网传输软件:打通数据孤岛,保障安全流通!
  • 202507_QQ_caidundun
  • DevExpress WinForms v25.1新版亮点:全新升级侧边导航布局
  • outlook大附件发送是什么?
  • 2025年内外网文件传输新范式:十大好用的内外网文件摆渡系统
  • 双分布函数热 LBM 模拟二维封闭方腔自然对流
  • asp.net中的wwwroot是什么
  • 了解IWebHostEnvironment : IHostEnvironment
  • 工业检测为啥首选黑白相机?4 个核心优势,彩色相机比不了 - 指南
  • 202504_CHIMA模拟_Shiro流量分析
  • 【通达信公式性能优化】:高级技巧揭秘,提升执行效率的10大策略 - Leone
  • 数分3
  • 基于模拟退火算法解决带容量限制车辆路径问题(CVRP)的MATLAB实现
  • 完整教程:分片后的聚合分页处理
  • H3C交换机取消分页,H3C交换机关闭分页功能
  • 基于Python+Vue开发的美容预约管理系统源码+运行步骤
  • 马大姐携手纷享销客启动CRM,打造快消行业数字化新标杆
  • FDS-400 土壤温湿电导率盐分传感器 四合一款 频域法测量
  • 接口压测方案
  • 产业投资集团如何科学选择HR系统?一文详解5大选型维度与主流产品对比
  • python处理Excel的单机小工具:自动合并相同数据的行, 并同时计算其他列的加和
  • 297、瑶瑟怨
  • 极飞科技携手纷享销客CRM实现业务全链条数字化
  • 实用指南:RESTful API:@RequestParam与@PathVariable实战对比
  • AT_arc194_d [ARC194D] Reverse Brackets
  • 202403_QQ_brutezip