Can u please Provide sample on Colorpickergallery control

Ribbon for WPF Forum

Posted 2 years ago by naveen gampa
Version: 21.1.3
Avatar

Hi, we are using Colorpickergallery to select to bind the colour to our controls.

we need like when ever we select particular control with RED colour the colour picker  should be displayed with red colour selected and when ever we select the control with green colour the green colour should be displayed as selected in colourpicker

1.The issue is we are able to see the previously selected colour in color picker.

2.can u pleae provide sample on this and we need sample how we use color picker gallery control from xaml.cs file and view model

3. we tried to bind the colour from viewmodel but unable to bind

Comments (1)

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

ColorPickerGallery uses SolidColorBrush as its items.  That being said, a Red SolidColorBrush might not be the same instance as another Red SolidColorBrush, so setting the ColorPickerGallery.SelectedItem to a Color or a SolidColorBrush instance not defined in the ColorPickerGallery might not allow a related item to be found.

Whereas if you do set the ColorPickerGallery.SelectedItem property to a SolidColorBrush instance that is defined in the gallery, selection updates should work.  This example shows the same instances used in a ListBox and as you change selection in the ListBox, it will update the ColorPickerGallery selection:

<ribbon:ColorPickerGallery x:Name="colorGallery" IsPreviewEnabled="True">
    <ribbon:ColorPickerGallery.ItemTemplate>
        <DataTemplate>
            <Rectangle Fill="{Binding BindsDirectlyToSource=True}" Width="20" Height="20" />
        </DataTemplate>
    </ribbon:ColorPickerGallery.ItemTemplate>
    <ribbon:ColorPickerGallery.CategorizedItemsSource>
        <x:Array Type="{x:Type SolidColorBrush}">
            <SolidColorBrush Color="White" />
            <SolidColorBrush Color="Black" />
            <SolidColorBrush Color="Red" />
            <SolidColorBrush Color="Green" />
            <SolidColorBrush Color="Blue" />
        </x:Array>
    </ribbon:ColorPickerGallery.CategorizedItemsSource>
</ribbon:ColorPickerGallery>

<ListBox x:Name="colorListBox"
            ItemsSource="{Binding ElementName=colorGallery, Path=CategorizedItemsSource}"
            SelectedItem="{Binding ElementName=colorGallery, Path=SelectedItem, Mode=TwoWay}" />

If you are having trouble getting the SelectedItem you set to be recognized (due to it being a different SolidColorBrush instance), you might have to iterate the ColorPickerGallery.Items and index find the item already in the gallery that matches color and then set that instance to the SelectedItem property.


Actipro Software Support

The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.