wpf颜色选择器

通过反射获取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>

结果:
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF(Windows Presentation Foundation)是一种用于构建 Windows 应用程序的框架。WPF 提供了许多自定义控件和样式,方便开发人员根据自己的需求定制界面。下面将以300字回答WPf自定义文件选择框的相关内容。 WPF提供了一个名为OpenFileDialog的类,用于打开文件选择对话框,但是该默认对话框的外观和功能可能无法满足特定需求。因此,我们可以使用WPF的自定义控件和样式来定制文件选择框。 首先,我们可以使用自定义的用户控件来构建文件选择框。用户控件可以包含一个TextBox用于显示选择的文件路径,以及一个Button用于触发文件选择对话框。通过绑定TextBox和Button的命令,我们可以实现当用户点击Button时弹出选择文件对话框,并将选择的文件路径显示在TextBox中。 其次,我们可以使用样式来美化文件选择框的外观。WPF的样式允许我们修改控件的外观和行为。我们可以通过修改背景颜色、边框样式、字体大小等属性,来定制文件选择框的外观。另外,通过添加动画效果和图片等元素,可以进一步增加文件选择框的吸引力。 最后,我们还可以为文件选择框添加一些附加功能,以增强用户体验。例如,可以为文件选择框增加文件类型过滤,只显示特定类型的文件。还可以为文件选择框增加多选功能,允许用户选择多个文件。此外,可以自定义文件选择框的标题、按钮文本等内容,让界面更加友好。 总之,通过WPF的自定义控件、样式和功能扩展,我们可以实现一个美观、灵活、易用的自定义文件选择框,以满足不同应用场景的需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值