How to get selected color name of WPF ColorPicker?
In WPF ColorPicker control, the Selected color’s name can be displayed by binding the color value of ColorPicker to Text value in TextBlock. The color name from color code can be obtained using SuchColor method using converter.
MainWindow.xaml:
<Window x:Class="ColorPickerColorName.MainWindow" 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:ColorPickerColorName" xmlns:syncfusion="http://schemas.syncfusion.com/wpf" mc:Ignorable="d" WindowStartupLocation="CenterScreen" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <local:ColorHexToStringConverter x:Key="ColorToWordKnownColors"> </local:ColorHexToStringConverter> </Window.Resources> <Window.DataContext> <local:ViewModel /> </Window.DataContext> <Grid> <Canvas> <syncfusion:ColorPicker HorizontalAlignment="Center" VerticalAlignment="Center" Color="{Binding ColorValue,Mode=TwoWay}" Canvas.Top="100" Canvas.Left="300"> </syncfusion:ColorPicker> <TextBlock Text="{ Binding ColorValue, Converter={StaticResource ColorToWordKnownColors}}" HorizontalAlignment="Center" VerticalAlignment="Center" Canvas.Top="105" Canvas.Left="450"/> </Canvas> </Grid> </Window>
View Model.cs
public class ViewModel : INotifyPropertyChanged { private Color _color = Colors.Green; public Color ColorValue { get { return _color; } set { _color = value; OnPropertyChanged("ColorValue"); } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string p) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(p)); } }
MainWindow.cs
public class ColorHexToStringConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Color color = (Color)value; //color name has been obtained string _Color = ColorEdit.SuchColor(color)[0]; return _Color; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
The following screenshot shows the output of sample with above code snippet.
Sample : View in GitHub
Conclusion
I hope you enjoyed learning about how to get selected color name of WPF ColorPicker.
You can refer to our WPF ColorPicker feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF ColorPicker example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!