【问题标题】:custom View Cell in listview having only button clickable列表视图中的自定义视图单元格只有按钮可点击
【发布时间】:2017-12-09 02:29:58
【问题描述】:

我想创建一个 viewcell,其中 stacklayout 包含 Frame 用于每行的边框。此框架包含标签和一个按钮。在此按钮上单击我必须更改屏幕。

当我在 iOS 模拟器上渲染该代码时发现一个问题,发现在选择任何行(即 viewcell)时,它会变灰并显示模糊的选定行,看起来很难看。我试图禁用解决了我的行选择问题的视图单元格,但它也禁用了按钮单击行,因为它是其中的一部分。

我只需要启用一个按钮并可供单击,并且 viewcell 上的其余部分不应该是可单击的或可供选择的。我必须使用框架作为边框。它还提供圆形边框,但建议我一些解决方案,我也可以用它来代替边框。

截图:

<ListView x:Name="lstView" RowHeight="150" SeparatorVisibility="None" IsPullToRefreshEnabled="False" >
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <StackLayout Padding="10,10,10,10">
          <Frame OutlineColor="Silver">
            <Grid>
              <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
              </Grid.RowDefinitions>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="100" />
              </Grid.ColumnDefinitions>
              <Label Text="Car Type" Grid.Row="0" Grid.Column="0" FontAttributes="Bold"/>
              <Label Text="{Binding CreatedDate}" Grid.Row="1" Grid.Column="0"/>
              <Label Text="{Binding TransactionId}" Grid.Row="2" Grid.Column="0"/>
              <StackLayout Grid.Row="0" Grid.Column="1" Orientation="Horizontal">
                <Label Text="Status" FontAttributes="Bold"/>
                <Label Text="{Binding Status}" FontAttributes="Bold"/>
              </StackLayout>
              <StackLayout Grid.Row="1" Grid.Column="1" Orientation="Horizontal">
                <Label Text="Customer Name" />
                <Label Text="{Binding CustName}" />
              </StackLayout>
              <Button Text="Button" Grid.Row="1" Grid.Column="2" Clicked="OnClicked" CommandParameter="{Binding DisplayName}"></Button>
            </Grid>
          </Frame>
          </StackLayout>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>

【问题讨论】:

  • 您可以在选择时移除listview项目的背景颜色。通过渲染ViewCell并设置SelectionStyle = UITableViewCellSelectionStyle.None
  • 这是 xamarin 形式。有什么办法可以在 xamarin 表单上做到这一点。或者请帮助我在渲染器中编写代码。我的目标是尽可能多地使用跨平台,因为我也针对 android 和 windows phone。
  • 另外,阿卡什,我只能在 iOS 端的 Cell 中配置 SelectionStyle 属性,而我可以在 Xamarin Forms Xaml 端像上面所做的那样在 Xamarin Forms Xaml 端做吗?

标签: listview xamarin.forms


【解决方案1】:

禁用选择 如果要禁用选择,请处理 ItemSelected 事件并将 SelectedItem 属性设置为 null:

SelectionDemoList.ItemSelected += (sender, e) => {
((ListView)sender).SelectedItem = null;

};

来源: https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/interactivity/

【讨论】:

    【解决方案2】:

    由于框架的原因,我在 Xamarin 形式的 Ios 端也发生了同样的效果。 注释框架标签 axml 代码你的代码可以正常工作 listview 这个效果会消失

    【讨论】:

    • 欢迎来到 SO。请阅读此how-to-answer 并按照那里的指南提供高质量的答案。
    【解决方案3】:

    如果您只想坚持使用 xamarin 表单,最简单的方法是将 ViewCell BackgroundColor 或 ViewCell.View 中的布局设置为白色。

    所以在这种情况下:

    <DataTemplate>
            <ViewCell>
              <StackLayout Padding="10,10,10,10" BackgroundColor="White">
    

    但由于有填充,可能会有一些空间透明,显示点击 ViewCell 时的触摸响应

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多