欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF 枚举与ComboBox 绑定

程序员文章站 2022-06-07 18:42:43
...

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;

方法五 (遇到再补充)

相关标签: WPF C# wpf c#