The reason for changing the color of the selected row, such as the WPF DataGrid listview

Source: Internet
Author: User
Tags wpf datagrid

In WPF, changing the color of the selected row is very simple, that is, using a trigger: for example, the DataGrid is used as an example: DataGrid. rowstyle style targettype = maid setterproperty = background value = white/style. triggers triggerproperty = ismouseover value = true setterproperty = background value = lightgray // trigger triggerprope

  

In WPF, changing the color of the selected row is very simple, that is, using a trigger: for example, using a DataGrid:

<DataGrid. rowstyle>
<Style targettype = "datagridrow">
<Setter property = "background" value = "white"/>
<Style. triggers>
<Trigger property = "ismouseover" value = "true">
<Setter property = "background" value = "lightgray"/>
</Trigger>
<Trigger property = "isselected" value = "true">
<Setter property = "background" value = "lightgray"/>
<Setter property = "foreground" value = "red"/>
</Trigger>
</Style. triggers>
</Style>
</DataGrid. rowstyle>

 

However, I tell you that if you do not set cellstyle, you will find that the above Code "looks like" does not work and is useless. This is the same for similar controls such as listview.

In fact, the above Code already works. Isselected is indeed triggered. Where is the problem?

The answer is correct on cellstyle. The background of rowstyle is changed, but cellstyle is not changed. You cannot see it.

That is to say, rowstyle is very embarrassing, because cellstyle is on it. So it will be overwritten. The reason is that black people, white people, and black clothes look black.

The change method is as follows:

Add cellstyle:

<DataGrid. cellstyle>
<Style targettype = "datagridcell">
<Style. triggers>
<Trigger property = "isselected" value = "true">
<Setter property = "background" value = "red"> </setter>
</Trigger>
</Style. triggers>
</Style>
</DataGrid. cellstyle>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.