WPF Color Picker
Here, I am going to demonstrate how to use WPF Extended Toolkit color picker and how to store colors in the database.
Step 1- Add Reference to your program
To enable color picker to your program, you need to install WPF Extended Toolkit from NuGet Package Manager.
Right click on your Project >> select Manage NuGet Packages >> search for Extended WpfTool kit.
Step 2
Include xmlns:xctk="http://schemas/xceed.com/wpf/xaml/toolkit" reference in your XAML file.
Step 3 - Create Color Picker.
- <xctk:ColorPicker Name="cp" Grid.Column="1" Margin="161,377,563,33" DisplayColorAndName="True" SelectedColorChanged="cp_SelectedColorChanged_1" AvailableColorsSortingMode="HueSaturationBrightness" ></xctk:ColorPicker>
Step 4 - Convert hex value to long
- private void cp_SelectedColorChanged_1(object sender, RoutedPropertyChangedEventArgs<Color?> e)
- {
- if (cp.SelectedColor.HasValue)
- {
- Color C = cp.SelectedColor.Value;
- Red = C.R;
- Green = C.G;
- Blue = C.B;
- long colorVal = Convert.ToInt64(Blue * (Math.Pow(256, 0)) + Green * (Math.Pow(256, 1)) + Red * (Math.Pow(256, 2)));
- }
-
- }
Store this colorVal to the database.
Step 5 - Retrive from Database
- byte[] bytes = BitConverter.GetBytes(ItmMasModelList[i - 2].ColorKy);
- leftBtn.Background = new SolidColorBrush(Color.FromRgb(bytes[2], bytes[1], bytes[0]));
- leftBtn.Style = ItemNewButtonStyle;