通过反射获取System.Drawing.Color中所有的颜色供选择
颜色model:
public class NamedColor : ViewModelBase
{
private string _name;
public string Name
{
get => _name;
set => Set(() => Name, ref _name, value);
}
private string _friendlyName;
public string FriendlyName
{
get => _friendlyName;
set => Set(() => FriendlyName, ref _friendlyName, value);
}
private SolidColorBrush _color;
public SolidColorBrush Color
{
get => _color;
set => Set(() => Color, ref _color, value);
}
}
viewmodel:
using System;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Media;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Color = System.Drawing.Color;
namespace NetWpfTest
{
public class ColorWindowVM : ViewModelBase
{
private RelayCommand<RoutedEventArgs> _loadCommand;
private Window _window;
public RelayCommand<RoutedEventArgs> LoadCommand => _loadCommand ??= new RelayCommand<RoutedEventArgs>((args) =>
{
_window = args.Source as Window;
this.GetColors();
});
private NamedColor _selectColorBrush;
public NamedColor SelectColorBrush
{
get => _selectColorBrush;
set => Set(() => SelectColorBrush, ref _selectColorBrush, value);
}
private ObservableCollection<NamedColor> _colors = new ObservableCollection<NamedColor>();
public ObservableCollection<NamedColor> Colors
{
get=> _colors;
set => Set(() => Colors, ref _colors, value);
}
private void GetColors()
{
Colors.Clear();
StringBuilder stringBuilder = new StringBuilder();
foreach (PropertyInfo propertyInfo in typeof(Color).GetProperties())
{
try
{
if (propertyInfo.PropertyType.Name == "Color")
{
string name = propertyInfo.Name;
stringBuilder.Clear();
int index = 0;
foreach (char ch in name)
{
if (index != 0 && Char.IsUpper(ch))
{
stringBuilder.Append(' ');
}
stringBuilder.Append(ch);
index++;
}
var color = (Color)propertyInfo.GetValue(null);
// Instantiate a NamedColor object.
NamedColor namedColor = new NamedColor
{
Name = name,
FriendlyName = stringBuilder.ToString(),
Color = new SolidColorBrush(System.Windows.Media.Color.FromRgb(color.R, color.G, color.B))
};
Colors.Add(namedColor);
}
}
catch (Exception ex)
{
int aa = 1;
}
}
int a = 0;
}
}
}
界面:
<Window x:Class="NetWpfTest.ColorWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:NetWpfTest" xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
mc:Ignorable="d" d:DataContext="{d:DesignInstance local:ColorWindowVM}"
Title="ColorWindow" Height="450" Width="800">
<b:Interaction.Triggers>
<b:EventTrigger EventName="Loaded">
<b:InvokeCommandAction Command="{Binding LoadCommand}" PassEventArgsToCommand="True"></b:InvokeCommandAction>
</b:EventTrigger>
</b:Interaction.Triggers>
<Window.Resources>
<DataTemplate DataType="{x:Type local:NamedColor}">
<Border>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Left">
<Border Background="{Binding Color}" Width="20" Height="20" Margin="0,0,20,0"></Border>
<TextBlock FontSize="16" FontFamily="Microsoft YaHei" Foreground="Black" Text="{Binding FriendlyName}"></TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</Window.Resources>
<Window.DataContext>
<local:ColorWindowVM></local:ColorWindowVM>
</Window.DataContext>
<Grid>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Height="50" HorizontalAlignment="Center">
<Border Width="50" Height="50" Background="{Binding SelectColorBrush.Color}" Margin="0,0,30,0"></Border>
<ComboBox ItemsSource="{Binding Colors}" SelectedItem="{Binding SelectColorBrush}" Width="300" VerticalContentAlignment="Center"></ComboBox>
</StackPanel>
</Grid>
</Window>
结果: