C# WPF 颜色选择器实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17089382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:32:44  来源:igfitidea点击:

WPF Color Picker Implementation

c#wpf

提问by

I have to create a color picker in my WPF application. When I click on any color, the code of that color should come in a textbox. I googled a lot but found nothing matching my requirement. Please share if you have done like this before.

我必须在我的 WPF 应用程序中创建一个颜色选择器。当我单击任何颜色时,该颜色的代码应出现在文本框中。我用谷歌搜索了很多,但没有找到符合我要求的东西。如果您以前这样做过,请分享。

采纳答案by Sean Cogan

As Jodha said, you should use the Color Picker Control from the WpfToolkit Extended. Implementing the Color Picker Control is easy, simply do something like this:

正如 Jodha 所说,您应该使用 WpfToolkit Extended 中的颜色选择器控件。实现颜色选择器控件很容易,只需执行以下操作:

Put this in your Window object:

把它放在你的 Window 对象中:

xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"

And this wherever you want your color picker.

这就是您想要的颜色选择器。

<xctk:ColorPicker Name="ClrPcker_Background" SelectedColorChanged="ClrPcker_Background_SelectedColorChanged"></xctk:ColorPicker>

Then, all you have to do is use the SelectedColorChanged event to change the text in the textbox, like this:

然后,您所要做的就是使用 SelectedColorChanged 事件来更改文本框中的文本,如下所示:

private void ClrPcker_Background_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color> e)
{
    TextBox.Text = "#" + ClrPcker_Background.SelectedColor.R.ToString() + ClrPcker_Background.SelectedColor.G.ToString() + ClrPcker_Background.SelectedColor.B.ToString();
}

Hope this helps!

希望这可以帮助!

回答by JSJ

You can check the Color Picker Controlof WpfToolKit Extended. This toolkit has many useful controls.

您可以检查拾色器控制WpfToolKit扩展。这个工具包有许多有用的控件。

回答by Nitesh

Have a look at ColorBoxcontrol at codeplex. You can also create Linear and Radial gradients with it.

在 codeplex 上查看ColorBox控件。您还可以使用它创建线性和径向渐变。