XAML ListView中的MenuFlyoutItem命令绑定

XAML MenuFlyoutItem Command Binding in ListView

我在项目中使用的是MVVM light,在NoteListPage中有一个ListView。在 ListView 中,我定义了 2 个 MenuFlyoutItem,我想将每个 MenuFlyoutItem 绑定到我在视图模型中创建的命令。

这是我的 NoteListPage.xaml 中的一些细节:

DataContext="{Binding NoteListPage, Source={StaticResource Locator}}">

在我的视图模型中,我有:

    public ObservableCollection<Note> NoteList
    {
        get { return _noteList; }

        set { Set(() => NoteList, ref _noteList, value); }
    }

    public ICommand DeleteComamand { get; private set; }
    public ICommand EditCommand { get; private set; }

我将 ItemSource 绑定到 NoteList,

<ListView  x:Name="NoteListView" ItemsSource="{Binding NoteList}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Border 
                    BorderBrush="White"
                    BorderThickness="2"
                    CornerRadius="5"
                    Width="360"
                    Margin="10,5" 
                    Tapped="Border_Tapped">

                    <FlyoutBase.AttachedFlyout>
                        <MenuFlyout>
                            <MenuFlyoutItem Text="Delete"
                        />

                            <MenuFlyoutItem Text="Edit"
                         />
                        </MenuFlyout>
                    </FlyoutBase.AttachedFlyout>

                    <StackPanel >
                        <TextBlock
                               FontSize="30" Text="{Binding NoteTitle}"/>
                        <TextBlock
                               FontSize="25"
                               TextWrapping="Wrap" Text="{Binding NoteContent}"/>
                    </StackPanel>

                </Border>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

但是我无法绑定 MenuFlyoutItem 命令,因为数据上下文的类型是 Model.Note

如何将 DeleteComamand 和 EditCommand 绑定到 MenuFlyoutItem,但在这种情况下 ListView ItemSource 仍然绑定到 NoteList?否则列表视图页面不显示任何项目 title/content.

您可以使用 ElementName 更改为父 DataContext:

<MenuFlyoutItem
    Text="Delete"
    Command="{Binding DataContext.DeleteComamand, ElementName=NoteListView}"/>
<Button 
      Name="BtnMessages"
      Style="{StaticResource HdIconButtonStyle}"
      Command="{Binding ButtonCommand}">                          

      <Button.Flyout>
          <Flyout>
              <ListView x:Name="myList"
                          ItemsSource="{Binding Notifications, Mode=TwoWay}"
                          Margin="10"
                          ItemContainerStyle="{StaticResource NotificationListViewStyle}">
                  <ItemsControl.ItemTemplate>
                      <DataTemplate>
                          <Grid Margin="12,9,0,8">
                              <Interactivity:Interaction.Behaviors>
                                  <Core:EventTriggerBehavior EventName="Tapped">
                                      <Core:InvokeCommandAction Command="{Binding DataContext.MyCommand, ElementName=myList}"/>
                                  </Core:EventTriggerBehavior>
                              </Interactivity:Interaction.Behaviors>
                              <Grid.RowDefinitions>
                                  <RowDefinition Height="Auto"></RowDefinition>
                                  <RowDefinition Height="30"></RowDefinition>
                              </Grid.RowDefinitions>
                              <Grid.ColumnDefinitions>
                                  <ColumnDefinition Width="255"></ColumnDefinition>
                              </Grid.ColumnDefinitions>
                              <TextBlock  Grid.Row="0"
                                              Grid.Column="0"
                                             Text="{Binding Message}" 
                                                  Style="{ThemeResource Style1}"
                                             TextWrapping="Wrap"/>
                              <TextBlock Grid.Row="1"
                                                 Grid.Column="0"
                                                 Margin="0,5,0,0"
                                                 Style="{StaticResource Style2}"
                                                 Text="29 minutes ago" 
                                                 TextWrapping="Wrap"/>
                          </Grid>
                      </DataTemplate>
                  </ItemsControl.ItemTemplate>
              </ListView>

          </Flyout>
      </Button.Flyout>
  </Button>