1

I want to invert the color (Fill) of a FrameworkElement in a ListViewItem when the ListViewItem is selected.

Problematic is that the FrameworkElement that should have the selected Color is in another DataTemplate in a nested DataTemplateSelector.

Example:

         <ListView>
            <ListViewItem>
                <ContentPresenter>
                    <ContentPresenter.ContentTemplateSelector>
                        <selector:IconTypeSelector>
                            <selector:IconTypeSelector.SuperImportantIcon>
                                <DataTemplate>
                                    <Rectangle Width="27"
                                           Height="27"
                                           Fill="{DynamicResource ColorThatShouldChange}"><!--This is what i want to have the font color of my ListViewElements  -->
                                    </Rectangle>
                                </DataTemplate>
                            </selector:IconTypeSelector.SuperImportantIcon>
                        </selector:IconTypeSelector>
                    </ContentPresenter.ContentTemplateSelector>
                </ContentPresenter>
            </ListViewItem>
        </ListView>

Best case scenario would be: My FrameworkElement (Rectangle in example) binds on the same color as the font color of text in a ListViewItemwhich gets inverted when selected.

Peter
  • 1,655
  • 22
  • 44
  • 1
    Could you bind the Rectangle's fill to the template's background/fill data? This may save some headache. You could then just change the background on an event of "OnSelected" (or whatever the event actually is). Put this as a comment as not quite sure if this will work. – D Hansen Nov 17 '16 at 12:47

2 Answers2

0

Read How to set a WPF ListView Selected Item color? and WPF ListView Highlight Color don't change

All you have to do is Bind your color to Bordes's BackGround.

Community
  • 1
  • 1
EgoPingvina
  • 734
  • 1
  • 10
  • 33
-2

you must be having the index number of the selected Item. on the Backend Code. Find the index and set System.Drawing,Color Of your Choice.

 for (int i = 0; i < list.Items.Count; i++)
    {
        if (list.Items[i].Bounds.Contains(e.Location) == true)
        {
            list.Items[i].BackColor = Color.Blue; // highlighted item
        }
        else
        {
            list.Items[i].BackColor = SystemColors.Window; // normal item
        }
    }