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();
        }
    }
相关推荐
稷下元歌1 天前
七天学会plc加机器视觉之AI 接入 外设模块开发全详细操作文档(全程配套视频按文档实操)
python·sql·qt·贪心算法·r语言·wpf·时序数据库
happyprince2 天前
11-Hugging Face Transformers 分布式与并行系统深度分析
分布式·c#·wpf
加号32 天前
【WPF】 基于 Canvas 读取并渲染 DXF 文件的技术指南
c#·wpf
AC赳赳老秦2 天前
用 OpenClaw 整理团队技术分享:自动提取 PPT 内容、生成文字稿、同步到知识库
开发语言·python·自动化·powerpoint·wpf·deepseek·openclaw
闪电悠米2 天前
黑马点评-秒杀优化-03_blocking_queue_async_order
数据库·分布式·oracle·junit·wpf·lua
kingwebo'sZone3 天前
WPF 在(WrapPanel父级使用可以自动换行)每个 TextBlock 显示一行数据(竖排,垂直)
wpf
闪电悠米3 天前
黑马点评-秒杀优化-02_lua_precheck
开发语言·redis·分布式·缓存·junit·wpf·lua
FuckPatience3 天前
WPF 获取一个控件某个依赖属性的默认绑定方式
wpf
加号33 天前
【WPF】 ListView 数据绑定:从列表呈现到复杂交互的完整实践
wpf·交互
闪电悠米4 天前
黑马点评-Redisson-01_why_redisson
java·服务器·网络·数据库·缓存·wpf