使用CustomViewCell的Xamarin.Forms ListView禁用所选项颜色

时间:2016-07-22 22:16:18

标签: c# listview xamarin.ios xamarin.forms

目前我有一个自定义ViewCell,其模板选择器绑定到ObservableCollection。 我已将自定义单元格模板绑定到我的Xamarin.Forms IOS ListView。

我的问题是我无法禁用所选项目的突出显示,只有在我通过MenuContext操作复制所选项目时才会显示灰色....

我尝试了我在互联网上找到的每个自定义渲染器:)我几乎放弃了似乎我无法解决这个问题也许有人可以给我一些信息。

也许即时使用错误的自定义渲染器。我是否需要这个问题的渲染器?

ViewCellRenderer

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>http://*</AllowedOrigin>
        <AllowedOrigin>https://*</AllowedOrigin>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>HEAD</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <ExposeHeader>Content-Range</ExposeHeader>
        <ExposeHeader>Content-Length</ExposeHeader>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

自定义ViewCell

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

的ListView

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

感谢您的时间!

1 个答案:

答案 0 :(得分:3)

问题不在于您的ViewCell,因此您无需自定义渲染器来解决问题。问题仅仅归因于ListView的行为。一旦您触摸ListView中的某个项目或项目的一部分,ListView将突出显示该项目,无论该单元格的内容是否像字符串一样简单,还是与其一样复杂在你的情况下。

要解决您的问题,您有两个选择

  1. 在处理后面的代码中 &#34; ListViewKalkulation_OnItemSelected&#34;使用

    ListViewKalkulation.SelectedItem = null; 
    
  2. 如果您确实想要为ViewCell使用客户渲染器,请尝试使用我从another solution获得的代码,尽管我尚未对其进行测试。

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