在列表视图中设置所选项目的样式

时间:2021-10-16 23:00:57

I have the following xaml:

我有以下xaml:

<ListView.ItemTemplate>
<DataTemplate>
    <Border x:Name="SetBorder" Background="Transparent">
        <Grid Margin="5" Width="130">
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Image Source="{Binding Path=Code, Converter={StaticResource SetCodeToImageConverter}}" MaxWidth="105">
                <Image.Style>
                    <Style>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=SetBorder, Path=Selector.IsSelectionActive}" Value="True">
                                <Setter Property="Image.Opacity" Value="1" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding ElementName=SetBorder, Path=IsMouseOver}" Value="True">
                                <Setter Property="Image.Opacity" Value="1" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding ElementName=SetBorder, Path=IsMouseOver}" Value="False">
                                <Setter Property="Image.Opacity" Value="0.5" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Image.Style>
            </Image>
            <TextBlock Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" 
                       Text="{Binding Path=Name}" TextWrapping="WrapWithOverflow" MaxWidth="120">
            </TextBlock>
        </Grid>
    </Border>
</DataTemplate>

I want the opacity of my image to be set to 1 when mouse is over (that part works) and when the item is selected (that part doesn't work). Any ideas ?

我希望当鼠标结束时(该部分工作)和选择项目时该图像的不透明度设置为1(该部分不起作用)。有任何想法吗 ?

1 个解决方案

#1


0  

Try this

尝试这个

<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=  {x:Type ListBoxItem}, Path=IsSelected}" Value="True">
   <Setter Property="Image.Opacity" Value="1" />
</DataTrigger>

I haven't tried it but it should work

我没有尝试过,但它应该工作

#1


0  

Try this

尝试这个

<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=  {x:Type ListBoxItem}, Path=IsSelected}" Value="True">
   <Setter Property="Image.Opacity" Value="1" />
</DataTrigger>

I haven't tried it but it should work

我没有尝试过,但它应该工作