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

时间:2014-10-30 16:08:43

标签: 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>

我的问题是,如果我点击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
};

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

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

6 个答案:

答案 0 :(得分:44)

您可以尝试使用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.
    ...
};

我在ListView(在Android设备上)测试了这个,它有足够的项目来滚动到混合中。我没有看到自动滚动行为,你设想将SelectedItem设置为空以击败突出显示的想法很有效。

答案 1 :(得分:16)

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

您可以直接在xaml上进行。 但是此方法不仅会禁用突出显示效果,还会禁用点击事件。

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

<ViewCell IsEnabled="false">
    //Your Item Layout Coding
</ViewCell>

此外,您还可以通过绑定来禁用/启用每个项目高亮效果:

<ViewCell IsEnabled="{Binding IsHighlightEnabled}">
    //Your Item Layout Coding
</ViewCell>

希望有所帮助,谢谢。

答案 2 :(得分: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");
  }
}

答案 3 :(得分:6)

YourList.ItemSelected+=DeselectItem;

 public void DeselectItem(object sender, EventArgs e)
  {
     ((ListView)sender).SelectedItem = null;
  }

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

答案 4 :(得分:5)

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

答案 5 :(得分:3)

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

NonSelectableListView.cs(在您的Forms项目中)

using System;
using Xamarin.Forms;

namespace YourProject
{
    public class NonSelectableListView : ListView
    {
        public NonSelectableListView()
        {
        }
    }
}

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;
            }
        }
    }

}