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

WPF案例展示

下拉框的数据绑定

枚举转换成对应的中文

设置数据上下文,设置数据源,设置数据格式转换器

<UserControl.DataContext><ViewModels:SelfViewModelAll />
</UserControl.DataContext>
<UserControl.Resources><Model:SelfModelC x:Key="SelfModelC" d:IsDataSource="True"/><DataTemplate x:Key="EnumItemTemplate"><TextBlock Text="{Binding Converter={StaticResource SelfModelC}}" /></DataTemplate>
</UserControl.Resources>

下拉框的设置:

 <ComboBox Name="ModelChoose" ItemsSource="{Binding ModelList //数据来源(链表),diagnostics:PresentationTraceSources.TraceLevel=High}" //测试SelectedItem="{Binding SelectedModel, Mode=TwoWay}" //选择的项ItemTemplate="{StaticResource EnumItemTemplate}" //转换器HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="3" Grid.Column="1" Height="25" />

 注意ModelList必须为属性,写Get/set方法并且是Public类型,否则会找不到

 public ObservableCollection<SelfModel> ModelList { get;  set ; } = new ObservableCollection<SelfModel>();private SelfModel _selectedModel;public SelfModel SelectedModel{get => _selectedModel;set => SetProperty(ref _selectedModel, value);}public SelfViewModelAll(){// 填充枚举值foreach (SelfModel type in Enum.GetValues(typeof(SelfModel))){ModelList.Add(type);}}

 枚举:

 public enum SelfModel : int{A= 0,B,C,}

 转换器:必须自己一个文档,否则写路径时找不到

 public class SelfModelC : IValueConverter{public object Convert(object value, Type targetType, object parameter, CultureInfo culture){if (value is SelfModel model){switch (model){case SelfModel.A:return "AA";case SelfModel.C:return "CC";case SelfModel.B:return "BB";default:return string.Empty;}}return string.Empty;}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){throw new NotImplementedException();}}

http://www.xdnf.cn/news/13474.html

相关文章:

  • Spring @Scope, @Lazy, @DependsOn, @Required, @Lookup
  • 阿里云可观测 2025 年 5 月产品动态
  • 2506C++,C++时间库与C时间
  • java使用WebMagic架构写个分布式爬虫
  • React Vue 编译/运行流程
  • 入门Scikit-learn:让机器学习像呼吸一样自然!
  • thinkphp 一个系统在同一个域名下,一个文件夹下如何区分多站点——穷人的精致规划——仙盟创梦IDE
  • 开疆智能ModbusTCP转Canopen网关连接三菱FX5u配置案例
  • 解决idea创建SpringBoot项目,会自动创建.git文件导致commit两极分化的现象。
  • 软件测试之基于博客系统项目的功能测试
  • OpenAI推出o3-Pro模型
  • Weblogic 弱口令-任意文件读取
  • 【uniapp】小程序中input输入框的placeholder-class不生效
  • 【慧游鲁博】【11】后端 · Spring Boot 集成 Python 配置
  • 跨链/Layer2交易所架构:全链互操作与ZK-Rollup优化
  • 快速上手驭码CodeRider二
  • 【VBA】把目录及子目录下所有doc/docx转换为pdf格式
  • 【一文理解】下采样与上采样区别
  • DAY 27 函数专题2:装饰器
  • 3.2 组帧
  • 嵌入式学习笔记DAY35(数据库)
  • 少量数据达到更好效果
  • 跌穿20万辆,更猛烈的价格战却导致销量暴跌,难怪电车内讧了!
  • 基于OpenCV的滑动验证码缺口识别全流程解析(2025企业级方案)
  • 基于强化学习的智能调度系统:从理论到实践
  • VSomeip库- 编译使用
  • Dify-6: 部署
  • HTB 赛季8靶场 - TombWatcher
  • 制造业数智化转型:从行业痛点到R²AIN SUITE AI一体化解决方案
  • 算法题(166):占卜DIY