在MenuItem上使用DataTrigger在Xamarin.Forms中显示不同的状态

时间:2017-06-01 00:10:53

标签: xamarin.forms menuitem datatrigger xamarin.forms.listview

我正在尝试在Xamarin.Forms中的ViewCell内的MenuItem上使用DataTrigger。 ListView显示会话列表。如果删除了对话,我希望菜单项显示“恢复”。如果未删除会话,我想显示“删除”。

在构建时我收到错误:

  

错误:位置12:49。无法解析菜单上的Clicked

<?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>

1 个答案:

答案 0 :(得分:0)

你的问题在于这一行:

<Setter Property="Clicked" Value="OnDelete" />

删除此行 - MenuItem没有Clicked属性。 Clicked是一个事件。

您应该绑定Clicked标记中的MenuItem事件,例如:

<MenuItem CommandParameter="{Binding Id}" Command="{Binding OnDelete}">