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();
        }
    }
相关推荐
松☆9 小时前
终章:构建完整生态——Flutter + OpenHarmony 分布式应用开发全景指南(含性能调优与发布实践)
flutter·wpf
松☆9 小时前
高阶实战:基于 Flutter 的 OpenHarmony 分布式软总线多设备协同应用开发
wpf
松☆9 小时前
终极挑战:Flutter 应用在 OpenHarmony 上实现跨设备无缝流转(Continuation)与软总线协同
flutter·wpf
她说彩礼65万9 小时前
WPF SynchronizationContext的使用
wpf
云雾J视界9 小时前
分布式AI框架选型困局:SintolRTOS vs Ray vs Horovod,性能压测全解析
tensorflow·wpf·horovod·ray·分布式ai·sintolrtos
豫狮恒1 天前
OpenHarmony Flutter 分布式多模态交互:融合音视频、手势与环境感知的跨端体验革新
flutter·wpf·openharmony
豫狮恒1 天前
OpenHarmony Flutter 分布式数据共享实战:从基础存储到跨设备协同
flutter·wpf·openharmony
500841 天前
鸿蒙 Flutter 隐私合规:用户授权中心与数据审计日志
flutter·华为·开源·wpf·音视频
豫狮恒1 天前
OpenHarmony Flutter 分布式软总线实战:跨设备通信的核心技术与应用
flutter·wpf·harmonyos
Hello.Reader1 天前
Flink SQL 的 LIMIT 子句语义、坑点与实战技巧
sql·flink·wpf