ColorPicker icon indicating copy to clipboard operation
ColorPicker copied to clipboard

PortableColorPicker in DataGridCell: Can't switch between primary to secondary color

Open Kruemelino opened this issue 2 years ago • 5 comments

Hi,

thank you for your work.

I've added a PortableColorPicker to a DataGridTemplateColumn of a DataGrid. Everything looks fine so far, but I can't switch between the primary and secondary colors.

What am I doing wrong? If I use the PortableColorPicker outside of the DataGrid, the switch between the colors is possible. Even if I add the StandardColorPicker into the DataGridTemplateColumn everything is ok.

Here is my example:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfAppTest"
        xmlns:cp="clr-namespace:ColorPicker;assembly=ColorPicker"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Window.Resources>
        <XmlDataProvider x:Key="MockList" XPath="/MockObjects/*" >
            <x:XData >
                <MockObjects xmlns="">
                    <MockObject />
                </MockObjects>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>

    <Grid DataContext="{Binding Source={StaticResource MockList}}">
        <DataGrid ItemsSource="{Binding Mode=Default, XPath=/MockObjects/MockObject}" 
                  AutoGenerateColumns="False">
            
            <DataGrid.Columns>

                <DataGridTemplateColumn Header="PortableColorPicker">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <cp:PortableColorPicker ShowAlpha="True" Height="14"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="StandardColorPicker">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <cp:StandardColorPicker ShowAlpha="True" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>

Thank you für your help

Addition: The selected items will not work in this case. image

Kruemelino avatar Aug 06 '22 15:08 Kruemelino

I'm experiencing exactly the same issue described, (NET 4.8).

I can't see anything out of place:

<colorpicker:PortableColorPicker
    Width="40" Height="20"
    Margin="5 2"
    Name="ColorPicker"
    ColorState="{Binding Color, Mode=TwoWay, Delay=20}"
    ShowAlpha="{Binding ShowAlpha}"
    UseHintColor="True" HintColor="Transparent"
    ToolTip="{Binding ElementName=ColorPicker,Path=HintColor}"
/>

Not sure if I'll have to use the source code directly to find out what's happening.

Edit: Forgot to mention that my issue is happening inside node from TreeView control.

JellyBitz avatar Sep 12 '22 07:09 JellyBitz

It was something to do with the Focus happening inside ListView/ListBox and all sort controls like that.

My solution at the moment, is about overriding the style from their items, or the item you're working on. At my case a Treeview so it would looks like :

<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="Focusable" Value="false"/>
    </Style>
</TreeView.ItemContainerStyle>

JellyBitz avatar Sep 14 '22 06:09 JellyBitz

hi.

I looked at your solution. It also works in the DataGrid.

<DataGrid.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Focusable" Value="False"/>
    <Style>
</DataGrid.CellStyle>

In the meantime I have created another solution using ItemsControl. The popup works in its ItemTemplate. In addition, I developed my own implementation of the PortableColorPicker. The PrimaryColor and SecondaryColor are displayed.

image

Kruemelino avatar Sep 14 '22 15:09 Kruemelino

Hi, sorry for not responding, we'll take a look at that. Thanks for investigation!

flabbet avatar Sep 14 '22 15:09 flabbet

The solution given didn't work on my treeview after all. I find out my issue is about Popup focus used into a TreeView.

So the other way around (after reading it was something to do with WPF bugs) end up like this:

PortableColorPicker.xaml

...
<popup PreviewLostKeyboardFocus="FixPopup_PreviewLostKeyboardFocus" ... >
...

PortableColorPicker.cs

private void FixPopup_PreviewLostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
{
    if (e.NewFocus is System.Windows.Controls.TreeViewItem)
    {
        if (popup.IsOpen)
            e.Handled = true;
    }
}

JellyBitz avatar Sep 16 '22 06:09 JellyBitz