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();
        }
    }
相关推荐
dalong107 小时前
WPF:Modbus 状态监控
wpf·modbus
He BianGu1 天前
【WPF-VisionMaster】机器视觉通用平台V5.0版本发行说明
opencv·c#·wpf·halcon·机器视觉·visionmaster
fogota4 天前
Emoji与Segoe MDL2 Assets的比较
c#·wpf
fogota4 天前
C# WPF 按钮加载字体图标 / 动物头像
c#·.net·wpf
fogota4 天前
C# WPF 按钮加载系统图标 / 动物头像
c#·.net·wpf
军训猫猫头4 天前
C# 封装 MySQL 自动写入与查询工具类(基于 MySqlConnector)
mysql·c#·.net·wpf
gis开发之家5 天前
Spring Boot 4 定时任务与异步线程池:从 @Scheduled 到高并发任务编排(生产级实战)
java·spring boot·后端·wpf·spring boot4
clz13145216 天前
如何设计一个优雅可靠的金融级联机接口
wpf
闲云自留地7 天前
云平台存储管理员:Cinder 创建挂载卷 + Swift 分布式存储原理
分布式·wpf·swift
敲代码的嘎仔8 天前
从集群锁失效到异步领券:我用分布式锁 + Redisson + MQ 把领券接口 RT 从 200ms 压到 15ms
java·数据库·redis·分布式·缓存·ai·wpf