[英]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.