ComboBox下拉框项重写

public partial class SecondPage : Window
    {
        public SecondPage()
        {
            InitializeComponent();
            this.Loaded += this.SecondPage_Loaded;
        }

        private void SecondPage_Loaded(object sender, RoutedEventArgs e)
        {
            CustomColorsList colorsList = new CustomColorsList();
            this.cmbColor.ItemsSource = colorsList;
        }
    }

    public class CustomContainer : INotifyPropertyChanged
    {

        private string _customColors;

        public string CustomColors
        {
            get { return _customColors; }
            set { _customColors = value; }
        }

        private void Notify(string name)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class CustomColorsList : List<CustomContainer>
    {
        public CustomColorsList()
        {
            CustomContainer custom = null;
            Type type = typeof(Brushes);
            var info = type.GetProperties();
            foreach (var property in info)
            {
                custom = new CustomContainer();
                custom.CustomColors = property.Name;
                Add(custom);
            }
        }
    }

xaml:

<ComboBox Name="cmbColor" Width="100" Height="30" Margin="77,37,101,194">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Rectangle Fill="{Binding CustomColors}" Width="20" />
                        <TextBlock Text="{Binding CustomColors}" VerticalAlignment="Center" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
原文地址:https://www.cnblogs.com/kelei12399/p/2608661.html