我正在使用Xamarin.Forms和XAML,并且正在尝试创建一个存储产品列表的应用程序。我将产品列表放在ListView中。这很好。这是我的XAML:
<ListView x:Name="listSushi"
ItemsSource="{x:Static local:myListSushi.All}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
RowHeight="{StaticResource rowHeight}"
>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Padding="5, 5, 0, 5"
Orientation="Horizontal"
Spacing="15">
<StackLayout>
<Image Source="{Binding ImageSource}" />
</StackLayout>
<StackLayout Padding="0, 0, 0, 0"
VerticalOptions="Center"
HorizontalOptions="FillAndExpand">
<Label Text="{Binding Name}"
Font="Bold, Medium" />
<Label Text="{Binding Description}"
Font="Small"/>
</StackLayout>
<StackLayout Orientation="Horizontal"
Padding="0, 0, 10, 0">
<Button Text=" - "
HorizontalOptions="EndAndExpand"
VerticalOptions="FillAndExpand"
Command="{Binding DeleteSushiCommand}"
CommandParameter="{Binding Name}"
/>
<Label VerticalOptions="Center"
Text="{Binding Number,StringFormat='{0}'}"
TextColor="Black"/>
<Button Text=" + "
HorizontalOptions="EndAndExpand"
VerticalOptions="FillAndExpand"
Command="{Binding AddSushiCommand}"
CommandParameter="{Binding Name}"
/>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
我有一个问题,如果我单击listView的一个单元格,该单元格将突出显示,并保持突出显示状态。我尝试使用xaml.cs中的此代码禁用它
listSushi.ItemSelected+= (object sender, SelectedItemChangedEventArgs e) => {
// don't do anything if we just de-selected the row
if (e.SelectedItem == null) return;
// do something with e.SelectedItem
((ListView)sender).SelectedItem = null; // de-select the row
};
但是,当我触摸一个单元格时,现在我的列表会自动滚动。真奇怪
有谁知道这是一个错误还是一个修复程序,例如是否存在可以禁用突出显示的属性?
您可以尝试使用ItemTapped事件,即
listSushi.ItemTapped += (object sender, ItemTappedEventArgs e) => {
// don't do anything if we just de-selected the row.
if (e.Item == null) return;
// Optionally pause a bit to allow the preselect hint.
Task.Delay(500);
// Deselect the item.
if (sender is ListView lv) lv.SelectedItem = null;
// Do something with the selection.
...
};
我已经在ListView(在Android设备上)上对此进行了测试,该列表具有足够的项目以将滚动带入混合。我没有看到自动滚动的行为,并且您想要将SelectedItem设置为null以击败突出显示的想法非常有用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句