[英]Unable to capture Xamarin ListView GroupHeader click event
我想在 ListView 中获取 GroupHeaderTemplate 的选定项目。 这当前位于 Master-Detail 设置的 MasterPage 中。 如何在 Xamarin Forms 中捕获 ListView GroupHeader 点击事件?
在这里,我有一个StackLayout
x:Name=GroupHeader
,我在其上添加了GestureRecognizers
并向其添加了一个Command
。 这已绑定到我的 ViewModel,以下是我的代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="MyApp.Views.MasterDetail.BaseMasterDetailPageMaster"
Title="Master"
x:Name="SlideMenuPage">
<StackLayout>
<ListView x:Name="SectionsListView"
SeparatorVisibility="None"
HasUnevenRows="true"
IsGroupingEnabled="true" x:FieldModifier="public">
<ListView.Header>
<Grid BackgroundColor="#03A9F4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="80"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="10"/>
</Grid.RowDefinitions>
<Label
Grid.Column="1"
Grid.Row="2"
Text="AppName"
Style="{DynamicResource SubtitleStyle}"/>
</Grid>
</ListView.Header>
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<StackLayout x:Name="GroupHeader" Orientation="Horizontal" Padding="5" BackgroundColor="AliceBlue">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Path=BindingContext.ShowDetailCommand, Source={x:Reference SectionsListView}}" CommandParameter="{Binding .}"/>
</StackLayout.GestureRecognizers>
<Label Text="{Binding Title}" TextColor="{StaticResource NavyBlue}" BackgroundColor="Red" FontSize="20" FontFamily="Gotham-Medium" VerticalOptions="Center" HorizontalOptions="StartAndExpand"/>
<Button BackgroundColor="Transparent" Image="{Binding StateIcon}" HeightRequest="10" Clicked="HeaderTapped" CommandParameter="{Binding .}" WidthRequest="15" HorizontalOptions="End" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="15,10" HorizontalOptions="FillAndExpand">
<Label VerticalOptions="FillAndExpand" VerticalTextAlignment="Center" Text="{Binding ItemName}" d:Text="{Binding .}" FontSize="20" FontFamily="Gotham-Book"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
我的 ViewModel 具有以下内容:
public ICommand ShowDetailCommand { get; set; }
//Constructor
public MyAppViewModel() {
ShowDetailCommand = new Command(ExpandHeader);
//ShowDetailCommand = new Command<MyAppViewModel>(ExpandHeader);//I have tried this, but didn't help.
}
public void ExpandHeader(object obj)
{
/*Some Logic*/
}
这是在 MasterPage 内的 Master-Detail 页面设置中。 请帮忙。 如果您需要更多信息,请告诉我。
谢谢!
您的 GroupViewModel 中是否定义了ShowDetailCommand
?
如果是,则需要通过Command="{Binding ShowDetailCommand"
<StackLayout x:Name="GroupHeader" Orientation="Horizontal" Padding="5" BackgroundColor="AliceBlue">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ShowDetailCommand}" CommandParameter="{Binding .}"/>
</StackLayout.GestureRecognizers>
...
</StackLayout>
更新:
你可以尝试这样改变:
在 page.cs 中设置BindingContext
:
MyAppViewModel viewModel = new MyAppViewModel(Navigation,this);
BindingContext = viewModel;
然后在您的 xaml 中将ItemSource
绑定到列表视图:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="MyApp.Views.MasterDetail.BaseMasterDetailPageMaster"
Title="Master"
x:Name="SlideMenuPage">
<StackLayout>
<ListView x:Name="SectionsListView" ItemsSource="{Binding ExpandedSections}"
SeparatorVisibility="None"
HasUnevenRows="true"
IsGroupingEnabled="true" x:FieldModifier="public">
<ListView.Header>
<Grid BackgroundColor="#03A9F4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="80"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="10"/>
</Grid.RowDefinitions>
<Label
Grid.Column="1"
Grid.Row="2"
Text="AppName"
Style="{DynamicResource SubtitleStyle}"/>
</Grid>
</ListView.Header>
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<StackLayout x:Name="GroupHeader" Orientation="Horizontal" Padding="5" BackgroundColor="AliceBlue">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Path=BindingContext.ShowDetailCommand, Source={x:Reference SlideMenuPage}}" CommandParameter="{Binding .}"/>
</StackLayout.GestureRecognizers>
<Label Text="{Binding Title}" TextColor="{StaticResource NavyBlue}" BackgroundColor="Red" FontSize="20" FontFamily="Gotham-Medium" VerticalOptions="Center" HorizontalOptions="StartAndExpand"/>
<Button BackgroundColor="Transparent" Image="{Binding StateIcon}" HeightRequest="10" Clicked="HeaderTapped" CommandParameter="{Binding .}" WidthRequest="15" HorizontalOptions="End" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="15,10" HorizontalOptions="FillAndExpand">
<Label VerticalOptions="FillAndExpand" VerticalTextAlignment="Center" Text="{Binding ItemName}" d:Text="{Binding .}" FontSize="20" FontFamily="Gotham-Book"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
你可以删除MyAppPageRef.SetListViewData(ExpandedSections);
在您的视图模型中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.