ColorPicker Control In UWP (Windows 10 Fall Creators Update Features)

A ColorPicker control is a new control in UWP programming. ColorPicker control allows the user to select the colors from the color spectrum or specify a color in RGB format.

Note

This control will be supported on Windows Insider latest build version, public version will be available October 17 (Windows 10 fall creators update version) onwards.

Let’s see how to implement this control and some of the important properties and the events.

ColorSpectrumShape

ColorSpectrumShape: This property is used to select the display(shape) style of the control, right now two styles are available: Box and Ring. Based on the style , the control will be display edto the GUI. The below images showRing and Box style format.

 

IsColorPreviewVisible

This property is used to show the preview of the color which user selects. If the mouse moves in the color spectrum control, the right side (Rectangle) box color will change.

IsColorChannelTextInputVisible

This property is used to show the RGB value to the user or get the input value from the user. Move the mouse on the color spectrum  color value changes  automatically to RGB value and displays in the RGB area. In other ways user can enter the RGB value, based on the RGB color value, it will display in the color spectrum area.( RGB area ref below image) . 

This property is set as false , it wont' display to the user.

IsHexInputVisible

This property is used to show the color value as a Hexadecimal (ref below image) value . This property depends on the IsColorChannelTextInputVisible ( set as false , IsHexInputVisiable also it won't display to the user). 

 

ColorChanged Event

This event is handling the ColorChanged value in the color spectrum control. It will trigger a color value change in the color spectrum.
It has two arguments: ColorPicker and ColorChangedEventArgs.
  1. private void CtrlPicker_OnColorChanged(ColorPicker sender, ColorChangedEventArgs args)  
  2.         {  
  3.   
  4.         }  
ColorPicker - ColorPicker(sender) object contains the control information in which control event gets fired.
ColorChangedEventArgs - args object contains the user-selected color value and old color value information.

Let's see a simple example: When the color value is changing, it automatically changes the rectangle box color 

System Requirements

  • Windows 10 Insider Build latest
  • Windows 10 Insider latest SDK
  • Visual Studio 2017
Open VisualStudio -> File -> Visual C# -> Windows Universal -> Blank App(Universal Windows)

XAML code changes 
  1. <Page  
  2.     x:Class="FallCreatorUpdate.Views.ColorCtrl"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:local="using:FallCreatorUpdate.Views"  
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  8.     mc:Ignorable="d">  
  9.   
  10.     <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">  
  11.         <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  12.   
  13.             <Grid.RowDefinitions>  
  14.                 <RowDefinition Height="Auto"/>  
  15.                 <RowDefinition Height="Auto"/>  
  16.                 <RowDefinition Height="Auto"/>  
  17.             </Grid.RowDefinitions>  
  18.   
  19.             <Grid.ColumnDefinitions>  
  20.                 <ColumnDefinition Width="Auto"/>  
  21.                 <ColumnDefinition Width="Auto"/>  
  22.             </Grid.ColumnDefinitions>  
  23.   
  24.             <StackPanel Grid.Row="0" Margin="20,10,0,0" Orientation="Horizontal" Grid.Column="0">  
  25.                 <CheckBox Name="ChkColorPreview" Content="IsColorPreviewVisible" Margin="20,10,0,0" IsChecked="True"/>  
  26.                 <CheckBox Name="ChkColorTextInput" Content="IsColorChannelTextInputVisible"  
  27.                           Margin="20,10,0,0" IsChecked="True"/>  
  28.                 <CheckBox Name="ChkColorHexInput" Content="IsHexInputVisible" Margin="20,10,0,0" IsChecked="True"/>  
  29.                 <CheckBox Name="ChkAlpha" Content="IsAlpha" Margin="20,10,0,0" IsChecked="True"/>  
  30.                 <StackPanel Margin="10,10,0,0" Orientation="Horizontal">  
  31.                     <RadioButton GroupName="CtlStyle" Content="Box" Tag="1" Click="ButtonBase_OnClick" IsChecked="True"/>  
  32.                     <RadioButton GroupName="CtlStyle" Content="Ring" Tag="2" Click="ButtonBase_OnClick"/>  
  33.                 </StackPanel>  
  34.             </StackPanel>  
  35.   
  36.             <ColorPicker Name="CtrlPicker" Grid.Row="1" Grid.Column="0" ColorSpectrumShape="Ring"  
  37.                          IsAlphaEnabled="{Binding ElementName=ChkAlpha,Path=IsChecked}"  
  38.                          IsColorPreviewVisible="{Binding ElementName=ChkColorPreview,Path=IsChecked}"  
  39.                          IsHexInputVisible="{Binding ElementName=ChkColorHexInput ,Path=IsChecked}"  
  40.                          IsColorChannelTextInputVisible="{Binding ElementName=ChkColorTextInput,Path=IsChecked}"  
  41.                          ColorChanged="CtrlPicker_OnColorChanged"/>  
  42.   
  43.             <StackPanel Grid.Row="1" Grid.Column="1">  
  44.                 <StackPanel Orientation="Vertical">  
  45.                     <TextBlock Text="Color Change" FontSize="25" Margin="10,10,10,10"/>  
  46.                     <Rectangle Name="NewRectColor" Width="300" Height="300" Fill="Brown"/>  
  47.                 </StackPanel>  
  48.             </StackPanel>  
  49.         </Grid>  
  50.     </ScrollViewer>  
  51. </Page>  
Viewmodel code changes
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Runtime.InteropServices.WindowsRuntime;  
  6. using Windows.Foundation;  
  7. using Windows.Foundation.Collections;  
  8. using Windows.UI.Xaml;  
  9. using Windows.UI.Xaml.Controls;  
  10. using Windows.UI.Xaml.Controls.Primitives;  
  11. using Windows.UI.Xaml.Data;  
  12. using Windows.UI.Xaml.Input;  
  13. using Windows.UI.Xaml.Media;  
  14. using Windows.UI.Xaml.Navigation;  
  15.   
  16. // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238  
  17.   
  18. namespace FallCreatorUpdate.Views  
  19. {  
  20.     /// <summary>  
  21.     /// An empty page that can be used on its own or navigated to within a Frame.  
  22.     /// </summary>  
  23.     public sealed partial class ColorCtrl : Page  
  24.     {  
  25.         public ColorCtrl()  
  26.         {  
  27.             this.InitializeComponent();  
  28.         }  
  29.   
  30.         private void ButtonBase_OnClick(object sender, RoutedEventArgs e)  
  31.         {  
  32.             if (sender is RadioButton)  
  33.             {  
  34.                 var ctrInfo = (RadioButton) sender;  
  35.   
  36.                 int val = Convert.ToInt32(ctrInfo.Tag);  
  37.   
  38.                 if (val == 1)  
  39.                 {  
  40.                     CtrlPicker.ColorSpectrumShape = ColorSpectrumShape.Box;  
  41.                 }  
  42.                 if (val == 2)  
  43.                 {  
  44.                     CtrlPicker.ColorSpectrumShape = ColorSpectrumShape.Ring;  
  45.                 }  
  46.             }  
  47.         }  
  48.   
  49.   
  50.         private void CtrlPicker_OnColorChanged(ColorPicker sender, ColorChangedEventArgs args)  
  51.         {  
  52.            NewRectColor.Fill = new SolidColorBrush(args.NewColor);  
  53.         }  
  54.     }  
  55. }  
Output 
 
Move the mouse in the Color Spectrum control. Color will automatically change (Rectangle). This functionality was handled in the CtrlPicker_OnColorChanged function in the above code.
 
Bind in XMAL
 
We can bind the ColorPicker color property directly to the other control also; the below code explains about how to implement this feature 
  1. <ColorPicker Name="CtrlPicker" Grid.Row="1" Grid.Column="0" ColorSpectrumShape="Ring"  
  2.                          IsAlphaEnabled="{Binding ElementName=ChkAlpha,Path=IsChecked}"  
  3.                          IsColorPreviewVisible="{Binding ElementName=ChkColorPreview,Path=IsChecked}"  
  4.                          IsHexInputVisible="{Binding ElementName=ChkColorHexInput ,Path=IsChecked}"  
  5.                          IsColorChannelTextInputVisible="{Binding ElementName=ChkColorTextInput,Path=IsChecked}"/>  
  6.   
  7. Binding color into the Rectangle box
  8.   
  9.  <Rectangle Name="NewRectColor" Width="300" Height="300">  
  10.                         <Rectangle.Fill>  
  11.                             <SolidColorBrush Color="{x:Bind CtrlPicker.Color,Mode=OneWay}"/>  
  12.                         </Rectangle.Fill>  
  13.                     </Rectangle>    
Conclusion

I hope you understood how to use Color picker control.

Up Next
    Ebook Download
    View all
    Learn
    View all