【问题标题】:How can I change background behind border (ListView.ItemTemplete->datatemplete) when I hover or click?当我悬停或单击时,如何更改边框 (ListView.ItemTemplate->datatemplate) 后面的背景?
【发布时间】:2019-08-28 04:37:14
【问题描述】:

我的问题是当我单击或悬停在 ListViewItem 中时,它也显示银色背景:

enter image description here

这是我的代码 xaml:

  <ListView 
                        Margin="0,30,0,0"
                        Height="600"
                        ScrollViewer.VerticalScrollBarVisibility="Hidden"
                        ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                        Name="ListViewFC" ItemsSource="{Binding ListWords, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  
                        SelectedItem="{Binding SelectedItem, Mode=TwoWay}" >
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <Border
                                        Width="340"
                                        x:Name="Border"
                                        Height="80"
                                        Background="Pink"
                                        CornerRadius="15"
                                        BorderThickness="1"
                                                    >
                                        <Grid>
                                            <TextBlock 
                                                VerticalAlignment="Center"
                                                x:Name="txtContent"
                                                Foreground="Black"
                                                Text="{Binding Question1,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
                                                TextWrapping="NoWrap"
                                                Margin="30 0 0 0"
                                                FontSize="15"
                                                       />
                                        </Grid>
                                    </Border>
                                    <DataTemplate.Triggers>
                                        <DataTrigger
                                    Binding="{Binding RelativeSource={RelativeSource FindAncestor,   
                                    AncestorType={x:Type ListViewItem}}, Path=IsSelected}" Value="True">
                                            <Setter TargetName="Border" Property="Background" Value="White" />


                                        </DataTrigger>
                                    </DataTemplate.Triggers>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>

我希望当我将鼠标悬停或单击时它不显示银色背景。

请帮帮我。谢谢。

【问题讨论】:

    标签: c# wpf xaml listview listviewitem


    【解决方案1】:

    将此添加到您的ListView

    <ListView.Resources>
       <Style BasedOn="{StaticResource TextBlockStyle}" TargetType="{x:Type TextBlock}" />
       <Style BasedOn="{StaticResource ListViewItemStyle}" TargetType="{x:Type ListViewItem}" />    
    </ListView.Resources>
    

    然后将其添加到您的 ListView 之外(悬停时显示金色背景):

    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
              <Style.Triggers>
                  <Trigger Property="IsMouseOver" Value="True">
                      <Setter Property="Background" Value="Gold" />
                  </Trigger>
              </Style.Triggers>
         </Style>
    </ListView.ItemContainerStyle>
    

    【讨论】:

    • 查看我的新评论
    【解决方案2】:

    ListView 的默认 ControlTemplate 包含一个 Border 和 2X 的 Padding。因此,您必须像这样更改其模板

    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <ContentPresenter />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
    

    【讨论】:

    • 查看我的新评论
    • 你可以在你的visual studio中查看
    猜你喜欢
    • 1970-01-01
    • 2020-05-10
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 2014-08-05
    • 2012-11-17
    • 1970-01-01
    相关资源
    最近更新 更多