Как удалить эффект наведения ListView при использовании ItemTemplate - PullRequest
0 голосов
/ 30 ноября 2018

Не удалось найти решение, которое работает для моего сценария, где у меня есть ListView с ItemTemplate, подобным этому:

<Window x:Class="ListViewHover.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListView ItemsSource="{Binding Items}" SelectedIndex="0">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Id}"/>
                        <TextBlock Text="{Binding Text}"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

Код позади:

namespace ListViewHover
{
    using System.Collections.Generic;
    using System.Windows;

    public partial class MainWindow : Window
    {
        public MainWindow()
        {

            InitializeComponent();
            DataContext = this;
            Items = new List<Item> { new Item(1), new Item(2), new Item(3) };
        }

        public List<Item> Items { get; set; }
    }

    public class Item
    {
        public Item(int id)
        {
            Id = id;
        }

        public int Id { get; set; }
        public string Text { get => $"This is Item number {Id}"; }
    }
}

Если вы запустите этосписок имеет эффект наведения по умолчанию.Мне нужно, чтобы это исчезло, поэтому единственный эффект - это изменение фона при выборе элемента.

Я попытался указать ItemContainerStyle, а также применить триггеры стиля к ListViewItems, но не могу получить егоРабота.

1 Ответ

0 голосов
/ 30 ноября 2018

Можете ли вы использовать ItemsControl?

        <ItemsControl ItemsSource="{Binding Items}" >
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="local:Item">
                    <StackPanel>
                        <TextBlock Text="{Binding Id}"/>
                        <TextBlock Text="{Binding Text}"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

enter image description here

Если вы хотите написать ListView и переписать ItemContainerStyle, чтобы вы могли использовать этот код.

<Window.Resources>
    <!-- set SelectedBackgroundColor to Transparent when you do not need the background in selected items -->
    <Color x:Key="SelectedBackgroundColor">#00FFFFFF</Color>
    <Color x:Key="SelectedUnfocusedColor">#FFB2A3A2</Color>

    <!-- set the MouseOverColor to Transparent when you do not need the effect in the unselected items -->
    <Color x:Key="MouseOverColor" >#00FFFFFF</Color>

    <Style x:Key="ListViewItemStyle"
           TargetType="ListViewItem">
        <Setter Property="SnapsToDevicePixels"
                Value="true" />
        <Setter Property="OverridesDefaultStyle"
                Value="true" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="Border"
                            Padding="2"
                            SnapsToDevicePixels="true"
                            Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver" >
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                                      Storyboard.TargetProperty="(Panel.Background).
                (SolidColorBrush.Color)">
                                            <EasingColorKeyFrame KeyTime="0"
                                                                 Value="{StaticResource MouseOverColor}" />
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled" />
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected" />
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                                      Storyboard.TargetProperty="(Panel.Background).
                (SolidColorBrush.Color)">
                                            <EasingColorKeyFrame KeyTime="0"
                                                                 Value="{StaticResource SelectedBackgroundColor}" />
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="SelectedUnfocused">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                                      Storyboard.TargetProperty="(Panel.Background).
                (SolidColorBrush.Color)">
                                            <EasingColorKeyFrame KeyTime="0"
                                                                 Value="{StaticResource SelectedUnfocusedColor}" />
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ContentPresenter />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>

        <ListView ItemsSource="{Binding Items}"
                  ItemContainerStyle="{StaticResource ListViewItemStyle}">
            <ListView.ItemTemplate>
                <DataTemplate DataType="local:Item">
                    <StackPanel>
                        <TextBlock Text="{Binding Id}" />
                        <TextBlock Text="{Binding Text}" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

Код ядра меняет SelectedBackgroundColor на Transparent, когда вам не нужен фон в выбранных элементах, и изменяет MouseOverColor на Transparent, когда вам не нужен эффект в невыбранных элементах.

См .: https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/listview-styles-and-templates

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...