Search code examples
c#wpfbindingcolor-pickerxceed

Global WPF Toolkit Colorpicker StandardColors


For a project i'm using the Xceed WPF Toolkit colorpicker to let the users select a color. This colorpicker is used in different pages and with our own custom colors. Therefore i want to create one global style in the Application.xaml and then use the colorpicker on the different pages, however i cannot find a properly working example of how to create a global style WITH an array of StandardColors other that creating it in code and copy/pasting the code onto the differente pages.

Hopefully someone could help me out with this.

The way i got it working now is:

Xaml on every page/usercontrol:

<xctk:ColorPicker
    Name="ClrPcker_Background"
    Grid.Row="4"
    Grid.Column="1"
    Width="300"
    Margin="3"
    HorizontalAlignment="Left"
    SelectedColor="{Binding BackgroundColor, Mode=TwoWay, Converter={StaticResource BrushToColor}, UpdateSourceTrigger=PropertyChanged}"
    SelectedColorChanged="ClrPcker_Background_SelectedColorChanged"
    ShowRecentColors="True" />

Code behind every page/usercontrol:

    CustomColorList.Add(New Xceed.Wpf.Toolkit.ColorItem(Color.FromRgb(32, 32, 32), "DarkBackground"))
    CustomColorList.Add(New Xceed.Wpf.Toolkit.ColorItem(Color.FromRgb(60, 60, 59), "DarkLightBackground"))
    CustomColorList.Add(New Xceed.Wpf.Toolkit.ColorItem(Color.FromRgb(87, 87, 86), "LightGrayBackground"))
    CustomColorList.Add(New Xceed.Wpf.Toolkit.ColorItem(Color.FromRgb(231, 76, 60), "RedLight"))
    CustomColorList.Add(New Xceed.Wpf.Toolkit.ColorItem(Color.FromRgb(39, 174, 96), "GreenLight"))
    CustomColorList.Add(New Xceed.Wpf.Toolkit.ColorItem(Color.FromRgb(52, 152, 219), "BlueLight"))
    CustomColorList.Add(New Xceed.Wpf.Toolkit.ColorItem(Color.FromRgb(243, 156, 18), "OrangeLight"))
    CustomColorList.Add(New Xceed.Wpf.Toolkit.ColorItem(Color.FromRgb(104, 34, 27), "RedDark"))
    CustomColorList.Add(New Xceed.Wpf.Toolkit.ColorItem(Color.FromRgb(25, 110, 61), "GreenDark"))
    CustomColorList.Add(New Xceed.Wpf.Toolkit.ColorItem(Color.FromRgb(22, 64, 92), "BlueDark"))
    CustomColorList.Add(New Xceed.Wpf.Toolkit.ColorItem(Color.FromRgb(116, 74, 9), "OrangeDark"))

Idealy i can also use our global UsedColors as ColorItems that are also added to the Application.xaml

        <!--  Background colors  -->
        <Color x:Key="clrDarkBackground">#202020</Color>
        <Color x:Key="clrDarkLightBackground">#3C3C3B</Color>
        <Color x:Key="clrLightGrayBackground">#575756</Color>
        <SolidColorBrush x:Key="scbDarkBackground" Color="{StaticResource clrDarkBackground}" />
        <SolidColorBrush x:Key="scbDarkLightBackground" Color="{StaticResource clrDarkLightBackground}" />
        <SolidColorBrush x:Key="scbLightGrayBackground" Color="{StaticResource clrLightGrayBackground}" />

        <!--  Light Colors  -->
        <Color x:Key="clrRedLight">#E74C3C</Color>
        <Color x:Key="clrGreenLight">#27AE60</Color>
        <Color x:Key="clrBlueLight">#3498DB</Color>
        <Color x:Key="clrOrangeLight">#F39C12</Color>
        <SolidColorBrush x:Key="scbRedLight" Color="{StaticResource clrRedLight}" />
        <SolidColorBrush x:Key="scbGreenLight" Color="{StaticResource clrGreenLight}" />
        <SolidColorBrush x:Key="scbBlueLight" Color="{StaticResource clrBlueLight}" />
        <SolidColorBrush x:Key="scbOrangeLight" Color="{StaticResource clrOrangeLight}" />

        <!--  Dark Colors  -->
        <Color x:Key="clrRedDark">#68221B</Color>
        <Color x:Key="clrGreenDark">#196E3D</Color>
        <Color x:Key="clrBlueDark">#16405C</Color>
        <Color x:Key="clrOrangeDark">#744A09</Color>
        <SolidColorBrush x:Key="scbRedDark" Color="{StaticResource clrRedDark}" />
        <SolidColorBrush x:Key="scbGreenDark" Color="{StaticResource clrGreenDark}" />
        <SolidColorBrush x:Key="scbBlueDark" Color="{StaticResource clrBlueDark}" />
        <SolidColorBrush x:Key="scbOrangeDark" Color="{StaticResource clrOrangeDark}" />

Solution

  • You should consider creating a custom control that inherits from ColorPicker instead of having to add items to a list of every view.

    You can then for example add the colours to the control in its constructor.