Custom Color Picker And Dynamically Change The Grid Color For Windows 10 Universal App

Colorpicker help users to pick colors to place with their own methods to implement.

Let’s see the steps.

Create a new Windows 10 universal application and go to your MainPage.xaml.

Now add one listview control to list the colors and also add two controls inside the ListView, one is for color name and another one is for showing the color.

  1. <ListView x:Name="colorList" SelectionChanged="colorList_SelectionChanged">  
  2.     <ListView.ItemTemplate>  
  3.         <DataTemplate>  
  4.             <StackPanel Orientation="Horizontal">  
  5.                 <TextBlock Height="50" Width="50" Text="{Bindingcolor}"></TextBlock>  
  6.                 <Rectangle Height="50" Width="50" Fill="{BindingColorBrush}"></Rectangle>  
  7.             </StackPanel>  
  8.         </DataTemplate>  
  9.     </ListView.ItemTemplate>  
  10. </ListView>
Next go to code behind page and createcolors class to store the color properties like the following code.
  1. public class colors   
  2. {  
  3.     public string color   
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public Brush ColorBrush  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13. }  
  14. public void loadcolors()   
  15. {  
  16.     List < colors > mycolors = new List < colors > ();  
  17.     Typetype = typeof(Windows.UI.Colors);  
  18.     PropertyInfo[] Properties = type.GetRuntimeProperties().ToArray < PropertyInfo > ();  
  19.     foreach(var item in Properties)   
  20.     {  
  21.         string s = item.Name;  
  22.         Color c = (Color) item.GetValue(nullnull);  
  23.         colorsnewcolor = newcolors();  
  24.         mycolors.Add(newcolors()  
  25.                      {  
  26.             color = item.Name,  
  27.                 ColorBrush = newSolidColorBrush(c)  
  28.         });  
  29.     }  
  30.     colorList.ItemsSource = mycolors;  
The above code is used to get the list of system colors under Windows.UI namespace. The Color class contains a list of Properties each returning a particular color. The above code reads the Type and extracts all the properties from system color.

Next loops through each of these properties and adds them into our Colorslist by converting them into a ColorPickerBrush
object. Finally add the list of colors to our listview.

Now write the following code to get the user selected color and apply it to our grid.
  1. private void colorList_SelectionChanged(object sender, SelectionChangedEventArgs e)  
  2. {  
  3.     varcolor = (colors) e.AddedItems[0];  
  4.     myGrid.Background = color.ColorBrush;  
  5. }  
Run the app and see the expected output it looks like the following screen.

output

For more information on Windows 10 UWP, refer to my eBook:
Read more articles on Universal Windows Platform:


Similar Articles