0
votes

Currently I have a custom ViewCell with a template selector bound to a ObservableCollection. Ive bound the custom cell template to my Xamarin.Forms IOS ListView.

My issue is that i cant disable the highlighting of the Selected Item, the Grey color only appears if i duplicate the selected item via the MenuContext Actions....

Ive tried every Custom Renderer that i found on the Internet :) im nearly giving up seems like that i cant fix this Problem maybe somebody can give me some Information.

Maybe im using a wrong custom renderer. Do i need a renderer for this problem?

ViewCellRenderer

[assembly: ExportRenderer(typeof(GerätViewCell), typeof(NativeViewCellRenderer))]
namespace LindeXF.iOS.Renderer {

    public class NativeViewCellRenderer : ViewCellRenderer {

        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv) {
            var cell = base.GetCell(item, reusableCell, tv);
            if (cell != null)
                cell.SelectionStyle = UITableViewCellSelectionStyle.None;

            return cell;
        }
    }
}

Custom ViewCell

<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="LindeXF.Core.CustomTemplates.GerätViewCell"
             >
  <ViewCell.ContextActions>
    <MenuItem x:Name="MenuItemDuplizieren"
              CommandParameter="{Binding Id}"
              Clicked="OnDuplizierenClicked"
              Text="Duplizieren" />
    <MenuItem x:Name="MenuItemLöschen"
              CommandParameter="{Binding Id}"
              Clicked="OnLöschenClicked"
              Text="Löschen"
              IsDestructive="True" />
  </ViewCell.ContextActions>
  <Grid ColumnSpacing="0" BackgroundColor="White">
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="40"/>
      <ColumnDefinition Width="9*"/>
      <ColumnDefinition Width="7*"/>
      <ColumnDefinition Width="10*"/>
      <ColumnDefinition Width="5*"/>
      <ColumnDefinition Width="18*"/>
      <ColumnDefinition Width="10*"/>
      <ColumnDefinition Width="10*"/>
    </Grid.ColumnDefinitions>
    <BoxView BackgroundColor="{Binding BoxColor}" Grid.Column="0"/>
    <Label FontSize="14" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Text="{Binding Test}" Grid.Column="1"/>
    <Label FontSize="14" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Text="{Binding Test}" Grid.Column="2"/>
    <Label FontSize="14" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Text="{Binding Test}" Grid.Column="3"/>
    <Label FontSize="14" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Text="{Binding Test}" Grid.Column="4"/>
    <Label FontSize="14" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Text="{Binding Test}" Grid.Column="5"/>
    <Label FontSize="14" HorizontalTextAlignment="End" Margin="0,0,5,0" VerticalTextAlignment="Center" Text="{Binding Test}" Grid.Column="6"/>
    <Label FontSize="14" HorizontalTextAlignment="End" Margin="0,0,5,0" VerticalTextAlignment="Center" Text="{Binding Test}" Grid.Column="7"/>
  </Grid>
</ViewCell>

ListView

  <ListView  x:Name="ListViewKalkulation"
             Margin="1,0,1,1"
             ItemTapped="ListViewKalkulationText_OnItemTapped"
             ItemTemplate="{StaticResource TemplateSelector}"
             ChildAdded="ListViewKalkulation_OnChildAdded"
             ItemSelected="ListViewKalkulation_OnItemSelected"
             SeparatorVisibility="None"
             BackgroundColor="Silver"
             Grid.Row="1" />

Thanks for your Time!

1
Hello , Maybe you provide more informtation about the problem , would be nice to mention why you want to custome render your viewcell. a use case scenario would be nice :)Ahmad ElMadi
Hej, sorry i was thinking that the problem was clear :) i just want to disable the highlighting of the Listview items when interacting with them (the ugly grey color that wont go) i was thinking that maybe it isnt possible because im using a view cell as my Listview itemiNCEPTiON_

1 Answers

3
votes

The problem is not with your ViewCell, so you do not need a custom renderer to solve your problem. The problem is simply due to the behavior of the ListView. Once you touch an item, or part of an item in a ListView, the ListView will highlight it regardless of whether the cell's contents was as simple as a string, or as complex as it is in your situation.

To solve your problem you have two options

  1. In the code behind when handling "ListViewKalkulation_OnItemSelected" use

    ListViewKalkulation.SelectedItem = null; 
    
  2. If you do want to use a customer renderer for the ViewCell, try using this code which I got from another solution, although I have not tested it.

    public override UITableViewCell GetCell(Cell item, UITableView tv)
    {
        var cell = base.GetCell(item, tv);
    
        cell.SelectedBackgroundView = new UIView {
            BackgroundColor = UIColor.DarkGray,
        };
    
        return cell;
    }