2014-10-30 83 views
10

我正在使用Xamarin.Forms和XAML,并试图创建存储产品列表的应用程序。我把我的产品列表放在一个ListView中。这工作正常。这是我的XAML:如何在Xamarin.Forms中禁用ListView中的突出显示

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

但是,当我触碰电池,现在我的列表会自动滚动。这很奇怪。

有谁知道这是一个错误,还是知道一个修复,就像有一个属性,我可以禁用高亮?

+0

如果只有你接受了我的答案。 ;-) – 2017-03-23 17:14:45

回答

30

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

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

我有一个ListView(在Android设备上)测试这一点,有足够的项目,使滚动进入组合。我看不到自动滚动行为,并且将SelectedItem设置为null以击败突出显示的想法非常好。

5

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

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

    //USE THE VALUE 

    _selectedItem = null; 
    NotifyPropertyChanged("SelectedItem"); 
    } 
} 
+2

该解决方案适用于我。我已经设置了_selectedItem = null,但这还不够;添加NotifyPropertyChanged是关键(或者在我使用MVVM Light之后,RaisePropertyChanged)。请注意,我还必须在XAML视图中设置Mode = TwoWay(作为SelectedItem的属性)才能使其工作 - 尽管我相信TwoWay应该是Mode的默认值,但我必须明确地设置它。 – 2017-05-26 12:13:49

+1

在我的情况下使用棱镜,我做到了这样简单 '公众RecapListViewItem SelectedRecap { {{0;返回null; } set {OnPropertyChanged(“SelectedRecap”); } }' – 2017-07-13 17:27:09

1

在iOS上标记的解决方案并没有解决这个问题对我来说,我不得不创建一个CustomRenderer列表视图并使用它。

NonSelectableListView.cs(在表单中项目)

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

} 
8

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

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

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

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

此外,您还可以通过结合禁用/启用每个项目的高亮效果:

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

希望帮助,谢谢。

+1

这将禁用突出显示,但值得注意的是它也将禁用可能在ViewCell中的任何按钮单击事件 – Nick 2018-01-08 16:00:53

0
YourList.ItemSelected+=DeselectItem; 

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

这应该对您的情况有所帮助。 @dpfauwadel