在 MenuItem 上使用 DataTrigger 在 Xamarin.Forms 中显示不同的状态
Using DataTrigger on MenuItem to display different state in Xamarin.Forms
我正在尝试在 Xamarin.Forms 中的 ViewCell 内的 MenuItem 上使用 DataTrigger。 ListView 显示对话列表。如果对话被删除,我希望菜单项显示 "Restore"。如果对话没有被删除,我想显示"Delete"。
构建时出现错误:
Error: Position 12:49. Can't resolve Clicked on MenuItem
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="EventingVolunteers.MailboxBoxPage" Title="Mailbox - Box">
<ContentPage.Content>
<AbsoluteLayout x:Name="absLayout" VerticalOptions="FillAndExpand">
<ListView x:Name="listView" HasUnevenRows="true" ItemSelected="OnItemSelected" HeightRequest="{Binding Path=Height, Source={x:Reference absLayout}}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem CommandParameter="{Binding Id}">
<DataTrigger TargetType="MenuItem" Binding="{Binding Is_Deleted}" Value="false">
<Setter Property="Clicked" Value="OnDelete" />
<Setter Property="Text" Value="Delete" />
<Setter Property="IsDestructive" Value="True" />
<Setter Property="Icon" Value="ic_delete.png" />
</DataTrigger>
<DataTrigger TargetType="MenuItem" Binding="{Binding Is_Deleted}" Value="true">
<Setter Property="Clicked" Value="OnRestore" />
<Setter Property="Text" Value="Restore" />
<Setter Property="IsDestructive" Value="False" />
<Setter Property="Icon" Value="" />
</DataTrigger>
</MenuItem>
</ViewCell.ContextActions>
<StackLayout Orientation="Horizontal" BackgroundColor="White" Padding="10,10,10,10">
<StackLayout Spacing="0" Orientation="Horizontal" HorizontalOptions="StartAndExpand">
<StackLayout Orientation="Vertical">
<StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
<Label Text="{Binding Subject}" VerticalTextAlignment="Center" FontAttributes="Bold">
<Label.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding Is_Unread}" Value="true">
<Setter Property="TextColor" Value="#337ab7" />
</DataTrigger>
</Label.Triggers>
</Label>
</StackLayout>
<StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
<Label Text="{Binding Participant}" VerticalTextAlignment="Center" />
</StackLayout>
<StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
<Label Text="{Binding Last_Message_At}" VerticalTextAlignment="Center" />
</StackLayout>
</StackLayout>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ContentView x:Name="overlay" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All" IsVisible="True" BackgroundColor="#C0808080" Padding="10, 0">
<ActivityIndicator WidthRequest="110" HeightRequest="70" IsRunning="True" IsVisible="True" Color="Black" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
</ContentView>
</AbsoluteLayout>
</ContentPage.Content>
</ContentPage>
您的问题与此行有关:
<Setter Property="Clicked" Value="OnDelete" />
删除此行 - MenuItem 没有 Clicked
属性。 Clicked
是一个事件。
您应该绑定到 MenuItem
标签中的 Clicked
事件,例如:
<MenuItem CommandParameter="{Binding Id}" Command="{Binding OnDelete}">
我正在尝试在 Xamarin.Forms 中的 ViewCell 内的 MenuItem 上使用 DataTrigger。 ListView 显示对话列表。如果对话被删除,我希望菜单项显示 "Restore"。如果对话没有被删除,我想显示"Delete"。
构建时出现错误:
Error: Position 12:49. Can't resolve Clicked on MenuItem
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="EventingVolunteers.MailboxBoxPage" Title="Mailbox - Box">
<ContentPage.Content>
<AbsoluteLayout x:Name="absLayout" VerticalOptions="FillAndExpand">
<ListView x:Name="listView" HasUnevenRows="true" ItemSelected="OnItemSelected" HeightRequest="{Binding Path=Height, Source={x:Reference absLayout}}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem CommandParameter="{Binding Id}">
<DataTrigger TargetType="MenuItem" Binding="{Binding Is_Deleted}" Value="false">
<Setter Property="Clicked" Value="OnDelete" />
<Setter Property="Text" Value="Delete" />
<Setter Property="IsDestructive" Value="True" />
<Setter Property="Icon" Value="ic_delete.png" />
</DataTrigger>
<DataTrigger TargetType="MenuItem" Binding="{Binding Is_Deleted}" Value="true">
<Setter Property="Clicked" Value="OnRestore" />
<Setter Property="Text" Value="Restore" />
<Setter Property="IsDestructive" Value="False" />
<Setter Property="Icon" Value="" />
</DataTrigger>
</MenuItem>
</ViewCell.ContextActions>
<StackLayout Orientation="Horizontal" BackgroundColor="White" Padding="10,10,10,10">
<StackLayout Spacing="0" Orientation="Horizontal" HorizontalOptions="StartAndExpand">
<StackLayout Orientation="Vertical">
<StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
<Label Text="{Binding Subject}" VerticalTextAlignment="Center" FontAttributes="Bold">
<Label.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding Is_Unread}" Value="true">
<Setter Property="TextColor" Value="#337ab7" />
</DataTrigger>
</Label.Triggers>
</Label>
</StackLayout>
<StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
<Label Text="{Binding Participant}" VerticalTextAlignment="Center" />
</StackLayout>
<StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
<Label Text="{Binding Last_Message_At}" VerticalTextAlignment="Center" />
</StackLayout>
</StackLayout>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ContentView x:Name="overlay" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All" IsVisible="True" BackgroundColor="#C0808080" Padding="10, 0">
<ActivityIndicator WidthRequest="110" HeightRequest="70" IsRunning="True" IsVisible="True" Color="Black" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
</ContentView>
</AbsoluteLayout>
</ContentPage.Content>
</ContentPage>
您的问题与此行有关:
<Setter Property="Clicked" Value="OnDelete" />
删除此行 - MenuItem 没有 Clicked
属性。 Clicked
是一个事件。
您应该绑定到 MenuItem
标签中的 Clicked
事件,例如:
<MenuItem CommandParameter="{Binding Id}" Command="{Binding OnDelete}">