XAML - 附加到ListView的MenuFlyoutItem在WP8.1中不起作用

时间:2015-09-29 19:24:05

标签: xaml windows-phone-8.1 mvvm-light relaycommand flyout

我开发了一个使用 MVVM-Lght工具包通用应用

在页面上,我会显示评论列表。我希望用户可以添加新评论,编辑或删除现有评论。

要添加新评论,我在 CommandBar 上使用 AppBarButton ,它运行正常。

为了编辑和删除现有评论,我想显示一个 MenuFlyout ,它提供了2个项目:" 编辑 & #34;和" 删除 "。我可以显示 MenuFlyout ,但点击其项目时没有任何反应......

enter image description here

以下是我关注的 xaml 代码:

    <ListView x:Name="myCommentaires" 
              ItemsSource="{Binding Comments}"
              IsItemClickEnabled="True"
              SelectionMode="Single"
              SelectedItem="{Binding SelectedComment}"
              ContinuumNavigationTransitionInfo.ExitElementContainer="True">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="0,0,19,12"
                            HorizontalAlignment="Stretch">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <!-- 1. Author -->
                        <TextBlock Grid.Column="0"
                                   Text="{Binding name}"
                                   HorizontalAlignment="Stretch" VerticalAlignment="Center"
                                   TextAlignment="Left"
                                   Margin="0" 
                                   Foreground="{StaticResource ThemeBrush}"
                                   Style="{StaticResource ListViewItemSubheaderTextBlockStyle}" />
                        <!-- 2. Date -->
                        <TextBlock Grid.Column="1"
                                   Text="{Binding date, Converter={StaticResource DateToStringConverter}}"
                                   HorizontalAlignment="Stretch" VerticalAlignment="Center"
                                   TextAlignment="Right"
                                   Margin="0" 
                                   Foreground="{StaticResource ThemeBrush}"
                                   Style="{StaticResource ListViewItemSubheaderTextBlockStyle}" />
                    </Grid>
                    <!-- 3. Content -->
                    <TextBlock Text="{Binding content}"
                               TextAlignment="Left"
                               TextWrapping="Wrap"
                               Margin="0" 
                               Foreground="Black"
                               FontSize="20"
                               Style="{StaticResource GroupHeaderTextBlockStyle}" />
                    <!-- MenuFlyout - with Commands -->
                    <FlyoutBase.AttachedFlyout>
                        <MenuFlyout>
                            <MenuFlyout.MenuFlyoutPresenterStyle>
                                <Style TargetType="MenuFlyoutPresenter">
                                    <Setter Property="Background" Value="{StaticResource ThemeBrush}"/>
                                </Style>
                            </MenuFlyout.MenuFlyoutPresenterStyle>
                            <MenuFlyoutItem Text="Edit"
                                            Command="{Binding EditCommentCommand}"/>
                            <MenuFlyoutItem Text="Delete" 
                                            Command="{Binding DeleteCommentCommand}"/>
                        </MenuFlyout>
                    </FlyoutBase.AttachedFlyout>
                    <!-- Behavior -->
                    <i:Interaction.Behaviors>
                        <core:EventTriggerBehavior EventName="Tapped">
                            <local:OpenFlyoutAction />
                        </core:EventTriggerBehavior>
                    </i:Interaction.Behaviors>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

附加代码隐藏

    public class OpenFlyoutAction : DependencyObject, IAction
    {
        public object Execute(object sender, object parameter)
        {
            FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
            return null;
        }
    }

这是我的 ViewModel 代码:

    // RelaydCommands declarations
    ...
    public RelayCommand AddCommentCommand { get; set; }
    public RelayCommand EditCommentCommand { get; set; }
    public RelayCommand DeleteCommentCommand { get; set; }
    ...

    // RelayCommands are affected in the constructor
    ...
    AddCommentaireCommand = new RelayCommand(AddCommentaire);
    EditCommentaireCommand = new RelayCommand(EditCommentaire);
    DeleteCommentaireCommand = new RelayCommand(DeleteCommentaire);
    ...

    // Methods related to the RelayCommands
    private async void AddComment()
    {
        NavigationService.NavigateTo<EditCommentViewModel>(this, new object[] { _article, _titleStructure.Title }, true); 
    }

    private async void EditComment()
    {
        NavigationService.NavigateTo<EditCommentViewModel>(this, new object[] { _article, _titleStructure.Title, SelectedComment }, true);
    }

    private async void DeleteComment()
    {
        var test = SelectedComment;
    }

我没有遇到任何问题&#34; AddComment &#34;方法,从 AppBarButton 调用,但我从不输入方法&#34; EditComment &#34;和&#34; DeleteComment &#34;而我放置了断点。

我还尝试用 CallMethodAction 替换 MenuFlyoutItem 命令,但它没有改变任何内容:

    <!-- MenuFlyout - with CallMethodAction -->
    <FlyoutBase.AttachedFlyout>
        <MenuFlyout>
            <MenuFlyout.MenuFlyoutPresenterStyle>
                <Style TargetType="MenuFlyoutPresenter">
                    <Setter Property="Background" Value="{StaticResource ThemeBrush}"/>
                </Style>
            </MenuFlyout.MenuFlyoutPresenterStyle>
            <MenuFlyoutItem Text="Edit">
                <i:Interaction.Behaviors>
                    <core:EventTriggerBehavior EventName="Click">
                        <core:CallMethodAction MethodName="EditComment" />
                    </core:EventTriggerBehavior>
                </i:Interaction.Behaviors>
            </MenuFlyoutItem>
            <MenuFlyoutItem Text="Delete">
                <i:Interaction.Behaviors>
                    <core:EventTriggerBehavior EventName="Click">
                        <core:CallMethodAction MethodName="DeleteComment" />
                    </core:EventTriggerBehavior>
                </i:Interaction.Behaviors>
            </MenuFlyoutItem>
        </MenuFlyout>
    </FlyoutBase.AttachedFlyout>

=&GT;有谁能说我出了什么问题?

1 个答案:

答案 0 :(得分:0)

您正在列表视图中定义flyout,因此datacontext设置为Comments上的ViewModel属性。 这意味着找不到您正在使用的2个命令!

要解决此问题,您需要引用您的页面datacontext并使用它来查找命令。

<Page>提供x:Name="PageRoot"之类的名称,并将命令的绑定更改为

{Binding ElementName=PageRoot, Path=DataContext.EditCommentCommand}