【问题标题】:Xamarin Forms Listview: Deselecting selected item by checking with OnTapped EventXamarin Forms Listview:通过检查 OnTapped 事件取消选择所选项目
【发布时间】:2017-01-09 00:14:05
【问题描述】:

当我第二次点击我的项目时,我将能够取消选择它。 当我点击一个项目时,背景颜色变为蓝色。当我第二次点击列表中的同一项目时,我希望背景获得默认颜色。 您可以在这里找到我的代码,但它不起作用。

XAML

<StackLayout Margin="5,0,5,0" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand">
    <Frame Style="{StaticResource StandardFrameStyle}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
      <ListView x:Name="lst_pickList" ItemSelected="OnItemSelected" ItemTapped="OnItemTapped" ItemsSource="{Binding PickList}" HasUnevenRows="True" Style="{StaticResource StandardListViewStyle}">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <ViewCell.View>
                <Grid x:Name="grd_pickList" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
                  <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                  </Grid.RowDefinitions>
                  <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                  </Grid.ColumnDefinitions>

                  <Label x:Name="lbl_comment" Text="Comment:" FontAttributes="Bold" Grid.Row="0" Grid.Column="0" VerticalTextAlignment="Center" Style="{StaticResource StandardLabelStyle}" />
                  <Label x:Name="lbl_itemNo" Text="ItemNo:" FontAttributes="Bold" Grid.Row="0" Grid.Column="2" VerticalTextAlignment="Center" Style="{StaticResource StandardLabelStyle}" />
                  <Label x:Name="lbl_description" Text="Description:" FontAttributes="Bold" Grid.Row="1" Grid.Column="0" VerticalTextAlignment="Center" Style="{StaticResource StandardLabelStyle}" />
                  <Label x:Name="lbl_restant" Text="Restant:" FontAttributes="Bold" Grid.Row="1" Grid.Column="2" VerticalTextAlignment="Center" Style="{StaticResource StandardLabelStyle}" />

                  <Label x:Name="lbl_comment_binding" Text="{Binding Comment}" Grid.Row="0" Grid.Column="1" VerticalTextAlignment="Center" Style="{StaticResource StandardLabelStyle}" />
                  <Label x:Name="lbl_itemNo_binding" Text="{Binding ItemNo}" Grid.Row="0" Grid.Column="3" VerticalTextAlignment="Center" Style="{StaticResource StandardLabelStyle}" />
                  <Label x:Name="lbl_description_binding" Text="{Binding Description}" Grid.Row="1" Grid.Column="1" VerticalTextAlignment="Center" Style="{StaticResource StandardLabelStyle}" />
                  <Label x:Name="lbl_restant_binding" Text="{Binding Restant}" Grid.Row="1" Grid.Column="3" VerticalTextAlignment="Center" Style="{StaticResource StandardLabelStyle}" />
                </Grid>
              </ViewCell.View>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
    </Frame>
  </StackLayout>

代码隐藏

public void OnItemTapped(object sender, ItemTappedEventArgs args)
    {
        var newSelectedPick = args.Item as Pick;

        if(selectedPick != null && selectedPick.Id == newSelectedPick.Id)
        {
            lst_pickList.SelectedItem = null;
        }
        else
        {
            selectedPick = newSelectedPick;
        }
    }

【问题讨论】:

    标签: c# listview xamarin xamarin.forms deselect


    【解决方案1】:

    在名为 IsSelectedPick 对象上创建一个 boolean 属性,然后使用转换器将 Bind 设置为 grd_pickListBackgroundColor

    即:

    //Xaml

    <ListView.Resources>
      <ResourceDictionary>
        <local:BgConverter x:Key="BgConverter"/>
      </ResourceDictionary>
    </ListView.Resources>
    
    <Grid x:Name="grd_pickList" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="{Binding IsSelected,Converter={StaticResource BgConverter}}" >
    

    //转换器

    class BgConverter : IValueConverter
    {
    
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool &&(bool)value)
                return Color.Blue;
             else
                return Color.White;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

    //Xaml.cs

    public void OnItemTapped(object sender, ItemTappedEventArgs args)
    {
        var itm = args.Item as Pick;
        itm.IsSelected = !itm.IsSelected;
    }
    

    重要提示:确保 IsSelected 属性触发 OnPropertyChanged 事件。

    【讨论】:

      猜你喜欢
      • 2020-10-18
      • 2019-11-09
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 2013-07-19
      相关资源
      最近更新 更多