如何在Xamarin.Forms中禁用ListView上的突出显示

dpf*_*del 22 xamarin xamarin.forms

我正在使用Xamarin.Forms和XAML,我正在尝试创建一个存储产品列表的应用程序.我把我的产品列表放在ListView中.这很好用.这是我的XAML:

<ListView x:Name="listSushi"
        ItemsSource="{x:Static local:myListSushi.All}"
        SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
        RowHeight="{StaticResource rowHeight}"
        >
<ListView.ItemTemplate>
  <DataTemplate>
    <ViewCell>
      <ViewCell.View>
        <StackLayout Padding="5, 5, 0, 5"
                     Orientation="Horizontal"
                     Spacing="15">
          <StackLayout>
            <Image Source="{Binding ImageSource}" />
          </StackLayout>

          <StackLayout Padding="0, 0, 0, 0"
                       VerticalOptions="Center"
                       HorizontalOptions="FillAndExpand">                
                <Label Text="{Binding Name}"
                   Font="Bold, Medium" />
                <Label Text="{Binding Description}" 
                    Font="Small"/>
          </StackLayout>

          <StackLayout Orientation="Horizontal"
                        Padding="0, 0, 10, 0">
            <Button Text=" - " 
                    HorizontalOptions="EndAndExpand"
                    VerticalOptions="FillAndExpand"
                    Command="{Binding DeleteSushiCommand}"
                    CommandParameter="{Binding Name}"
                    />
            <Label VerticalOptions="Center" 
                   Text="{Binding Number,StringFormat='{0}'}"
                   TextColor="Black"/>
            <Button Text=" + " 
                    HorizontalOptions="EndAndExpand"
                    VerticalOptions="FillAndExpand" 
                    Command="{Binding AddSushiCommand}"
                    CommandParameter="{Binding Name}"
                    />
            </StackLayout>
        </StackLayout>
      </ViewCell.View>
    </ViewCell>
  </DataTemplate>
</ListView.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)

我只是问题,如果我点击我的listView的单元格,单元格突出显示,并保持高亮.我试图在xaml.cs中使用此代码禁用它

listSushi.ItemSelected+= (object sender, SelectedItemChangedEventArgs e) => {
    // don't do anything if we just de-selected the row
    if (e.SelectedItem == null) return; 
    // do something with e.SelectedItem
    ((ListView)sender).SelectedItem = null; // de-select the row
};
Run Code Online (Sandbox Code Playgroud)

但是当我触摸一个单元格时,现在我的列表会自动滚动.这很奇怪.

有没有人知道这是一个错误,还是知道一个修复,就好像有一个属性,我可以禁用突出显示?

Mar*_*ter 45

您可以尝试使用ItemTapped事件,即

listSushi.ItemTapped += (object sender, ItemTappedEventArgs e) => {
    // don't do anything if we just de-selected the row.
    if (e.Item == null) return;

    // Optionally pause a bit to allow the preselect hint.
    Task.Delay(500);

    // Deselect the item.
    if (sender is ListView lv) lv.SelectedItem = null;

    // Do something with the selection.
    ...
};
Run Code Online (Sandbox Code Playgroud)

我在ListView(在Android设备上)测试了这个,它有足够的项目可以将滚动带入混合.我没有看到自动滚动行为,你想要设置SelectedItem null以击败突出显示效果很好.

  • `Task.Delay(500)` 本身不会延迟。您必须等待它创建的任务:“await Task.Delay(500);”。 (2认同)

小智 16

我刚刚找到另一种禁用高亮效果的方法.我想与其他用户分享.

你可以直接在xaml上做.但是这种方法不仅禁用了高亮效果,还会禁用click事件.

您可以将ViewCell的IsEnabled属性设置为false.

<ViewCell IsEnabled="false">
    //Your Item Layout Coding
</ViewCell>
Run Code Online (Sandbox Code Playgroud)

此外,您还可以通过绑定禁用/启用每个项目突出显示效果:

<ViewCell IsEnabled="{Binding IsHighlightEnabled}">
    //Your Item Layout Coding
</ViewCell>
Run Code Online (Sandbox Code Playgroud)

希望有所帮助,谢谢.

  • 这将禁用突出显示,但值得注意的是,它还会禁用ViewCell中可能存在的任何按钮点击事件 (4认同)
  • @IanWarburton 它不会禁用滚动。ListView 本身上的设置“IsEnabled=false”可以,但如果在单个单元格上使用则不会。 (2认同)

小智 14

当前,我们可以将ListView.SelectionMode设置为None来执行此操作。 https://docs.microsoft.com/zh-cn/xamarin/xamarin-forms/user-interface/listview/interactivity


Riy*_*yas 7

YourList.ItemSelected+=DeselectItem;

 public void DeselectItem(object sender, EventArgs e)
  {
     ((ListView)sender).SelectedItem = null;
  }
Run Code Online (Sandbox Code Playgroud)

这应该对您的方案有所帮助.@dpfauwadel


jga*_*rza 6

我假设你正在使用MVVM.在这些情况下,我在使用它之后为属性分配了一个null.在您的viewmodel中,您似乎具有SelectedItem属性,因为您将它绑定到ListView的SelectedItem属性.所以我会做这样的事情:

private Product _selectedItem;
public Product SelectedItem
{
  get
  {
    return _selectedItem;
  }
  set
  {
    _selectedItem = value;

    //USE THE VALUE

    _selectedItem = null;
    NotifyPropertyChanged("SelectedItem");
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 这个解决方案对我有用.我已经设置了_selectedItem = null但这还不够; 添加NotifyPropertyChanged是关键(或者在我的情况下,因为我使用的是MVVM Light,所以是RaisePropertyChanged).请注意,我还必须在XAML视图中设置Mode = TwoWay(作为SelectedItem的属性)才能使其工作 - 即使我认为TwoWay应该是Mode的默认值,我必须明确设置它. (3认同)

Rob*_*sey 5

在 iOS 标记的解决方案没有为我解决它,我不得不为列表视图创建一个 CustomRenderer 并使用它。

NonSelectableListView.cs(在您的表单项目中)

using System;
using Xamarin.Forms;

namespace YourProject
{
    public class NonSelectableListView : ListView
    {
        public NonSelectableListView()
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

NonSelectableListViewRenderer.cs(iOS 项目中的 CustomRenderer)

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

using YourProject.iOS;
using YourProject;

[assembly: ExportRenderer(typeof(NonSelectableListView), typeof(NonSelectableListViewRenderer))]
namespace YourProject.iOS
{
    public class NonSelectableListViewRenderer : ListViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                // Unsubscribe from event handlers and cleanup any resources
            }

            if (e.NewElement != null)
            {
                // Configure the native control and subscribe to event handlers
                Control.AllowsSelection = false;
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)