G
G
GISoft2014-08-27 22:43:14
WPF
GISoft, 2014-08-27 22:43:14

How to make the background transparency of list items in Wpf?

Learning Wpf - ran into a problem. The search did not help because I have no idea where to dig.
The idea is this: Each element of the list is highlighted in a different color depending on the contents of one of the columns, and the even (odd) element of the list has a greater (less) transparency than the odd (even).
Here is my attempt. (In this case, of course, the transparency of the entire element changes along with the text, but only the background is needed.)

<Style x:Key="DropedItem" TargetType="ListViewItem">
            <Style.Triggers>
        	<Trigger Property="Selector.IsSelected" Value="True">
        		<Setter Property="FontWeight" Value="Bold"/>
    </Trigger>
                
    <DataTrigger Binding="{Binding [address-list]}" Value="plain-tarif">
        		<Setter Property="Background" Value="#B857DA1A" />
        		<Setter Property="Foreground" Value="#FF151515"/>
        	</DataTrigger>
      		
        	<DataTrigger Binding="{Binding [address-list]}" Value="drop-user">
        		<Setter Property="Background" Value="#B8DA1A1A" />
        		<Setter Property="Foreground" Value="White"/>
        	</DataTrigger>
                
                <DataTrigger Binding="{Binding [address-list]}" Value="admin">
                    <Setter Property="Background" Value="#B8DAAE1A" />
                    <Setter Property="Foreground" Value="#FF151515"/>
                </DataTrigger>

    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Opacity"  Value="0.5"/>
                </Trigger>

            </Style.Triggers>            
        </Style>

My result:
9b3990948669404ab9bce87ffb36335a.png
I also tried MultiTrigger, but the application crashes without explanation, apparently due to the fact that DataTrigger and Trigger do not get along together.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
p1x, 2014-09-04
@GISoft

I'm afraid that some triggers can not do. You can solve it by overriding the ControlTemplate and moving the trigger into it, like so:

<Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListViewItem">
                        <Grid>
                            <Rectangle x:Name="Background" Fill="{TemplateBinding Background}" />
                            <GridViewRowPresenter />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                                <Setter TargetName="Background" Property="Opacity"  Value="0.5"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

A
Anton Papin, 2014-12-06
@i_light

Rewrite the Setters in the right places like this:

<Setter Property="Background">
   <Setter.Value>
      <SolidColorBrush Color="#DAAE1A" Opacity="0.5"/>
   </Setter.Value>
</Setter>

In general, it is very useful sometimes to use the XAML syntax features that are inherited from XML :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question