WPF 枚举与ComboBox 绑定

11 篇文章 0 订阅

PS :一段时间不写WPF,有些技术都忘记具体怎么实现了。蛋疼啊。以后遇到一个技术就写下来吧。

方法 一 :XMAL 绑定

namespace DN.ORM
{
    public enum DatabaseTypes
    {
        /// <summary>
        /// https://www.runoob.com/mysql/mysql-create-database.html
        /// </summary>
        MYSQL,
        /// <summary>
        /// 
        /// </summary>
        SQLServer,
        /// <summary>
        /// 
        /// </summary>
        SQLite,
        /// <summary>
        /// 
        /// </summary>
        Oracle,
    }
}
            <ObjectDataProvider x:Key="KeyOfDatabaseTypes" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
                <ObjectDataProvider.MethodParameters>
                    <x:Type TypeName="database:DatabaseTypes" />
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
    <controls:MSComboBox x:Name="Txt_DataBaseType"
                         Width="300"
                         Margin="10,10"
                         controls:InfoElement.Necessary="True"
                         controls:InfoElement.Placeholder="MYSQL"
                         controls:InfoElement.Title="数据库类型"
                         controls:InfoElement.TitlePlacement="Left"
                         controls:InfoElement.TitleWidth="100"
                         ItemsSource="{Binding Source={StaticResource KeyOfDatabaseTypes}}" />

方法 二 :后台代码 一

       this.Cmb_DataBaseType.ItemsSource = Enum.GetValues(typeof(DatabaseTypes));
       this.Cmb_DataBaseType.SelectedItem = DatabaseTypes.MYSQL;
       this.Cmb_Charset.ItemsSource = Enum.GetValues(typeof(CharsetTypes));
       this.Cmb_Charset.SelectedItem = CharsetTypes.utf8;

方法 三 :后台代码 二

       List<City> list = new List<City>();
       list.Add(new City { ID = 1, Name = "上海" });
       list.Add(new City { ID = 2, Name = "北京" });
       list.Add(new City { ID =3, Name = "天津" });
       cmb_list.ItemsSource = list;
<ComboBox Name="cmb_list"  Height="23" DisplayMemberPath="Name" SelectedValuePath="ID" />

方法 四 :自定义ComboBox控件

    /// <summary>
    /// 提供类型选择的下拉框
    /// </summary>
    public class TypeComboBox : ComboBox
    {
        /// <summary/>
        public TypeComboBox()
        {
            ItemsSource = Source;
        }

        /// <summary>
        /// 在下拉框弹出窗口时发生
        /// </summary>
        protected override void OnDropDownOpened(EventArgs e)
        {
            ItemsSource = Source;
            base.OnDropDownOpened(e);
        }

        private IEnumerable Source => UIEditorServices.TypeProvideService.TypeNames;
    }
        public TypeProvideService()
        {
            this.TypeDict.Add(typeof(Rect), typeof(Rect).Name);
            this.TypeDict.Add(typeof(Point), typeof(Point).Name);
            this.TypeDict.Add(typeof(Size), typeof(Size).Name);
            this.TypeDict.Add(typeof(Brush), typeof(Brush).Name);
            this.TypeDict.Add(typeof(string), typeof(string).Name);
            this.TypeDict.Add(typeof(byte), typeof(byte).Name);
            this.TypeDict.Add(typeof(sbyte), typeof(sbyte).Name);
            this.TypeDict.Add(typeof(decimal), typeof(decimal).Name);
            this.TypeDict.Add(typeof(double), typeof(double).Name);
            this.TypeDict.Add(typeof(float), typeof(float).Name);
            this.TypeDict.Add(typeof(int), typeof(int).Name);
            this.TypeDict.Add(typeof(uint), typeof(uint).Name);
            this.TypeDict.Add(typeof(long), typeof(long).Name);
            this.TypeDict.Add(typeof(ulong), typeof(ulong).Name);
            this.TypeDict.Add(typeof(short), typeof(short).Name);
            this.TypeDict.Add(typeof(ushort), typeof(ushort).Name);

            this.TypeDict.Add(typeof(Thickness), typeof(Thickness).Name);
            this.TypeDict.Add(typeof(CornerRadius), typeof(CornerRadius).Name);
            //this.TypeDict.Add(typeof(ushort), typeof(ushort).Name);
            //this.TypeDict.Add(typeof(ushort), typeof(ushort).Name);
        }

        /// <summary/>
        public IEnumerable<string> TypeNames => TypeDict.Values;

方法五 (遇到再补充)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Daniel大妞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值