简体   繁体   English

选择项目时更改ListViewItem内容的颜色

[英]Change the color of ListViewItem Content when Item is selected

I want to invert the color ( Fill ) of a FrameworkElement in a ListViewItem when the ListViewItem is selected. 我想反转的颜色( Fill一的) FrameworkElement一个ListViewItem的时候ListViewItem选择。

Problematic is that the FrameworkElement that should have the selected Color is in another DataTemplate in a nested DataTemplateSelector . 问题在于应具有选定颜色的FrameworkElement在嵌套DataTemplateSelector中的另一个DataTemplate中。

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 ListViewItem which gets inverted when selected. 最好的情况是:My FrameworkElement (例如Rectangle )以与ListViewItem本的字体颜色相同的颜色绑定,该颜色在选定时会反转。

Read How to set a WPF ListView Selected Item color? 阅读如何设置WPF ListView选定项的颜色? and WPF ListView Highlight Color don't change WPF ListView高亮显示颜色不变

All you have to do is Bind your color to Bordes's BackGround. 您所要做的就是将颜色绑定到Bordes的BackGround。

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. 找到索引并设置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
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择ListViewItem时如何更改文本块的颜色? - How to change Textblock color when the ListViewItem is selected? 仅在选择ListViewItem时显示内容 - Displaying Content only when ListViewItem is Selected 当重新绘制并且光标在项目上方时,ListViewItem失去背景色 - ListViewItem loses background color when repainted and the cursor is over the item 选择Windows 8存储XAML ListViewItem高度时的高度 - Change the height of a Windows 8 store XAML ListViewItem height when it's selected 生成 ListViewItem 时,如何更改此“签入”按钮的颜色? - How can I change the color of this 'CheckIn' Button when the ListViewItem is generated? 强制ListViewItem背景颜色在绑定项目绑定时更改 - Force ListViewItem Background colour to change when bound item it is bound to changes 选择datagrid项时如何获取按钮并更改其颜色? - How to get the button and change its color when datagrid item selected? 单击未选中的列表视图项中的按钮并获得选中的项 - Clicking a button in a listviewitem that is not selected and getting selected item 更改填充时ListViewItem的背景颜色 - Change the background color of a ListViewItem on populate 如何在WP8中更改背景颜色和列表框所选项目的textbloxk内容的颜色? - How to change background color and color of textbloxk content of selected item of a listbox in WP8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM