WPF 一个简单的颜色选择器

本章讲述:WPF中,一个简单的颜色选择器的实现;

具体简单的界面显示引用可以参考地址:https://blog.csdn.net/BYH371256/article/details/90520134

使用外部依赖库文件:“ColorPicker.dll”;

下载地址:https://download.csdn.net/download/byh371256/10745273

1、XAML界面代码

<Window x:Class="ColorInput.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ColorPicker;assembly=ColorPicker"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="cprct" TargetType="{x:Type Rectangle}">
            <Setter Property="Width" Value="25"/>
            <Setter Property="Height" Value="25"/>
            <Setter Property="Margin" Value="1"/>
            <Setter Property="Stroke" Value="Gray"/>
            <Setter Property="StrokeThickness" Value="1"/>
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="Rectangle_PreviewMouseLeftButtonDown"/>
        </Style>
    </Window.Resources>
    <Border  VerticalAlignment="Center" Width="280" Background="#f0f0f0">
        <StackPanel>
            <WrapPanel HorizontalAlignment="Center">
                <Rectangle Style="{StaticResource cprct}" Fill="White"/>
                <Rectangle Style="{StaticResource cprct}" Fill="Gray"/>
                <Rectangle Style="{StaticResource cprct}" Fill="Yellow"/>
                <Rectangle Style="{StaticResource cprct}" Fill="Orange"/>
                <Rectangle Style="{StaticResource cprct}" Fill="HotPink"/>
                <Rectangle Style="{StaticResource cprct}" Fill="Peru"/>
                <Rectangle Style="{StaticResource cprct}" Fill="RoyalBlue"/>
                <Rectangle Style="{StaticResource cprct}" Fill="SkyBlue"/>
                <Rectangle Style="{StaticResource cprct}" Fill="Teal"/>
                <Rectangle Style="{StaticResource cprct}" Fill="Tomato"/>
                <Rectangle Style="{StaticResource cprct}" Fill="Black"/>
                <Rectangle Style="{StaticResource cprct}" Fill="Sienna"/>
                <Rectangle Style="{StaticResource cprct}" Fill="Gold"/>
                <Rectangle Style="{StaticResource cprct}" Fill="DarkBlue"/>
                <Rectangle Style="{StaticResource cprct}" Fill="Magenta"/>
                <Rectangle Style="{StaticResource cprct}" Fill="LimeGreen"/>
                <Rectangle Style="{StaticResource cprct}" Fill="Lime"/>
                <Rectangle Style="{StaticResource cprct}" Fill="Blue"/>
                <Rectangle Style="{StaticResource cprct}" Fill="Green"/>
                <Rectangle Style="{StaticResource cprct}" Fill="Red"/>
            </WrapPanel>
            <StackPanel Orientation="Horizontal" Height="30">
                <TextBlock Text="ARGB:" VerticalAlignment="Center" Margin="16,0,10,0"/>
                <TextBox Text="{Binding ElementName=cpicker, Path=SelectedColor, Mode=TwoWay}" VerticalAlignment="Center" Width="90"/>
                <Rectangle Stroke="Gray" StrokeThickness="1" Width="80" Height="22" Margin="10,4,4,4"
                           Fill="{Binding ElementName=cpicker, Path=SelectedBrush}"/>
            </StackPanel>
            <local:ColorPicker x:Name="cpicker" SelectedColorChanged="cpicker_SelectedColorChanged"
                   SelectedColor="{Binding ElementName=clrctrl, Path= ExSelectedColor,Mode=OneWayToSource}"
                   SelectedBrush="{Binding ElementName= clrctrl,Path=ExSelectedBrush, Mode=TwoWay}" />
        </StackPanel>
    </Border>
</Window>

2、后台逻辑代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ColorInput
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }
        private void Rectangle_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            Brush brush = (sender as Rectangle).Fill;
            Color c = (Color)ColorConverter.ConvertFromString(brush.ToString());
            cpicker.SelectedColor = c;
            if (ColorChangedEvent != null)
                ColorChangedEvent(this, c);
        }

        public event ColorChangedHandler ColorChangedEvent;

        private void cpicker_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
        {
            if (this.IsLoaded == false)
                return;
            if (ColorChangedEvent != null)
                ColorChangedEvent(this, e.NewValue);
        }

        public static readonly DependencyProperty ExSelectedColorProperty = DependencyProperty.Register(
                     "ExSelectedColor",
                     typeof(Color),
                     typeof(MainWindow),
                     new PropertyMetadata(Colors.Black));

        public Color ExSelectedColor
        {
            get { return (Color)GetValue(ExSelectedColorProperty); }
            set { SetValue(ExSelectedColorProperty, value); }
        }

        public static readonly DependencyProperty ExSelectedBrushProperty = DependencyProperty.Register(
                      "ExSelectedBrush",
                      typeof(Brush),
                      typeof(MainWindow),
                      new PropertyMetadata(Brushes.Black));

        public Brush ExSelectedBrush
        {
            get { return (Brush)GetValue(ExSelectedBrushProperty); }
            set { SetValue(ExSelectedBrushProperty, value); }
        }
    }

    public delegate void ColorChangedHandler(object sender, Color newColor);
}

3、效果图

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值