2014-10-30 55 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

相關問題