如何清除Xamarin.WPF中ListView的悬停和选择样式

如何清除Xamarin.WPF中ListView的悬停和选择样式,wpf,xamarin,xamarin.forms,Wpf,Xamarin,Xamarin.forms,以下是XAML: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/desig

以下是XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"             
             mc:Ignorable="d"
             x:Class="App1.MainPage">

    <ListView ItemsSource="{Binding TestList}" SeparatorVisibility="None" HasUnevenRows="True">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.View>
                        <ContentView>
                            <Label Text="{Binding TestName}"></Label>
                        </ContentView>
                    </ViewCell.View>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage>

下面是隐藏的代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace App1
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            this.BindingContext = this;
            TestList.Add(new Test() { TestName = "aaa" });
            TestList.Add(new Test() { TestName = "bbb" });
            TestList.Add(new Test() { TestName = "ccc" });
            TestList.Add(new Test() { TestName = "ddd" });
        }
        public ObservableCollection<Test> TestList { get; set; } = new ObservableCollection<Test>();
        public class Test { 
            public string TestName { get; set; }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Xamarin.Forms;
名称空间App1
{
//了解有关使自定义代码在Xamarin.Forms预览器中可见的更多信息
//参观https://aka.ms/xamarinforms-previewer
[设计时间可见(错误)]
公共部分类主页:ContentPage
{
公共主页()
{
初始化组件();
this.BindingContext=this;
Add(newtest(){TestName=“aaa”});
Add(newtest(){TestName=“bbb”});
添加(newtest(){TestName=“ccc”});
Add(newtest(){TestName=“ddd”});
}
公共ObservableCollection TestList{get;set;}=new ObservableCollection();
公共类测试{
公共字符串TestName{get;set;}
}
}
}
上述项目将在android和WPF中跨平台运行

在android中:

当项目悬停时,没有任何true样式。

当项目被选中时,它会将颜色更改为橙色,我可以通过更改标签的背景来更改它

嗯,在WPF中:

当选中或悬停该项目时,它将显示一个我不需要的蓝色矩形

我不想在WPF中选中或悬停项目时显示蓝色矩形,这就是我问这个问题的原因

我可能知道我应该在WPF程序中设置一些属性,但我不知道应该设置哪个属性


你能帮帮我吗?谢谢。

你试过这样的东西吗

         <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="OverridesDefaultStyle" Value="True"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListViewItem">
                            <ContentPresenter/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.ItemContainerStyle>


但它失去了所有默认行为。你可以在需要定制的情况下使用触发器和视觉状态,如鼠标悬停效果等

您可以尝试使用自定义渲染器来实现此效果

在WPF项目的
App.xaml
中定义
ListViewItem
样式:

<Application.Resources>
    <Style x:Key="LvItemStyle" TargetType="ListViewItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListViewItem">
                    <Border x:Name="border" Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <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="LightBlue" />
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="SelectedUnfocused">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="border"
                                                          Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                            <EasingColorKeyFrame KeyTime="0" Value="SkyBlue" />
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ContentPresenter/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

wpf:您可以覆盖listview的默认模板,以便删除悬停或选定的背景。@neeleshbodgal我试图在wpf中覆盖listviewitem模板,但它不起作用。xamarin中的Listview似乎与WPF中的Listview不同。xamarin的Listview没有ItemContainerStyle属性。我只尝试将样式添加到WPF程序的资源中,但仍然没有效果。似乎Xamarin.Forms版本WPF与原始WPF有所不同。
using System.Windows.Controls;
using WpfApp1;
using Xamarin.Forms.Platform.WPF;

[assembly: ExportRenderer(typeof(Xamarin.Forms.ListView), typeof(MyListView))]
namespace WpfApp1
{
   class MyListView:ListViewRenderer
   {
      protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e)
      {
          base.OnElementChanged(e);
          foreach (var item in Control.Children)
           {
            if (item is ListView)
             {
                var list = item as ListView;
                list.ItemContainerStyle = App.Current.Resources["LvItemStyle"] as System.Windows.Style;
             }
           }
      }

  }
}