WPF enumeration and ComboBox binding

PS: I haven't written WPF for a while, and some technologies have forgotten how to implement it. The egg hurts. Write it down when you encounter a technology in the future.

Method 1: XMAL binding

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}}" />

Method two: background code one

       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;

Method three: background code two

       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" />

Method 4: Customize the ComboBox control

    /// <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;

Method 5 (replenish when encountered)

Guess you like

Origin blog.csdn.net/weixin_41045657/article/details/115033280