XAML 是否可以仅当在数据网格的某些列中单击鼠标右键时才显示ContextFlyout菜单?

gmxoilav  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(106)

我有一个Syncfusion数据网格控件,我在其中包含了一个CellRightTapped事件和一个FlyoutBase。现在,只有在单击第3列时才运行CellRightTapped事件代码。
所以我想弄清楚的是,如果右键单击第3列,怎么会没有上下文菜单出现。
下面是我的XAML代码:

<sf:SfDataGrid x:Name="PasswdVw" Grid.Row="2" Grid.Column="2" Grid.RowSpan="2"
    ItemsSource="{Binding PassWrd}" SelectedRow="{Binding SelPassRws, Mode=TwoWay}">
   <FlyoutBase.ContextFlyout>
      <MenuFlyout>
         <MenuFlyoutItem Text="Copy User Name" Clicked="MenuFlyoutItem_Clicked"
          CommandParameter="UsrName"/>
         <MenuFlyoutItem Text="Copy Password" Clicked="MenuFlyoutItem_Clicked"
          CommandParameter="Passwd"/>
         <MenuFlyoutItem Text="Go To Web Site" Clicked="MenuFlyoutItem_Clicked"
          CommandParameter="WebSite"/>
         </MenuFlyout>
        </FlyoutBase.ContextFlyout>
        <sf:SfDataGrid.DefaultStyle>
            <sf:DataGridStyle RowBackground="white" SelectedRowTextColor="White"
            SelectionBackground="blue" HeaderRowBackground="#C5C5C5"
            HeaderRowTextColor="Black" HeaderRowFontAttributes="Bold"/>
        </sf:SfDataGrid.DefaultStyle>
        <sf:SfDataGrid.Columns>
            <sf:DataGridTextColumn MappingName="PassId" Visible="False"/>
            <sf:DataGridTemplateColumn MappingName="PassTitle" HeaderText="Title"
              CellPadding="10" HeaderTextAlignment="Center"  ColumnWidthMode="FitByCell">
            <sf:DataGridTemplateColumn.CellTemplate>
               <DataTemplate>
                 <local:HyperlinkLabel Text="{Binding PassTitle}"
                     BackgroundColor="White"
                     Url="{Binding PassUrl}" VerticalOptions="Center"/>
                 </DataTemplate>
               </sf:DataGridTemplateColumn.CellTemplate>
            </sf:DataGridTemplateColumn>
            <sf:DataGridTextColumn MappingName="PassUsrname" HeaderText="User Name"
               HeaderTextAlignment="Center" ColumnWidthMode="FitByCell"/>
            <sf:DataGridTextColumn MappingName="PassPassword" HeaderText="Password"
               HeaderTextAlignment="Center" Width="200"/>
            <sf:DataGridTextColumn MappingName="PassUrl" Visible="False"/>
            <sf:DataGridTextColumn MappingName="PassGrpName" HeaderText="Group"
               HeaderTextAlignment="Center" ColumnWidthMode="FitByCell"/>
            <sf:DataGridTextColumn MappingName="PassNotes" HeaderText="Notes" Width="100"
               HeaderTextAlignment="Center"/>
        </sf:SfDataGrid.Columns>
    </sf:SfDataGrid>

谢谢

aiqt4smr

aiqt4smr1#

可以通过在CellRightTapped事件中分配空值来限制ContextMenu。为了供您参考,我附上了代码片段。
代码片段:
XAML:

<FlyoutBase.ContextFlyout>
    <MenuFlyout x:Name="contextMenuItems">
        <MenuFlyoutItem Text="Copy User Name" Clicked="MenuFlyoutItem_Clicked" CommandParameter="UsrName"/>
        <MenuFlyoutItem Text="Copy Password"  Clicked="MenuFlyoutItem_Clicked" CommandParameter="Passwd"/>
        <MenuFlyoutItem Text="Go To Web Site" Clicked="MenuFlyoutItem_Clicked" CommandParameter="WebSite"/>
    </MenuFlyout>
</FlyoutBase.ContextFlyout>

C#:

private void dataGrid_CellRightTapped(object sender,DataGridCellRightTappedEventArgs e)

{

    if (e.RowColumnIndex.ColumnIndex == 3)
    {
        FlyoutBase.SetContextFlyout(dataGrid, null);
    }
    else
    {
        FlyoutBase.SetContextFlyout(dataGrid, contextMenuItems);
    }
}

相关问题