【问题标题】:Xamarin.Forms ListView with CustomViewCell disable Selected Item ColorXamarin.Forms ListView 与 CustomViewCell 禁用选定项颜色
【发布时间】:2016-11-26 22:06:53
【问题描述】:

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

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

我已经尝试了我在 Internet 上找到的每个自定义渲染器

也许我使用了错误的自定义渲染器。我需要一个渲染器来解决这个问题吗?

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

自定义 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  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" />

感谢您的宝贵时间!

【问题讨论】:

  • 您好,也许您提供了有关该问题的更多信息,很高兴提及您为什么要自定义渲染您的视单元。一个用例场景会很好:)
  • Hej,对不起,我认为问题很清楚 :) 我只想在与 Listview 项目交互时禁用它们的突出显示(不会去的丑陋的灰色)我在想也许这是不可能的,因为我使用视图单元作为我的 Listview 项目

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


【解决方案1】:

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

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

  1. 在代码后面处理时 “ListViewKalkulation_OnItemSelected”使用

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

【讨论】:

  • 抱歉回复晚了,你说的对!从来没有想过这个。谢谢这解决了我的问题!
猜你喜欢
  • 2017-12-17
  • 2018-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-15
  • 1970-01-01
  • 2019-07-14
相关资源
最近更新 更多