2017-10-19 24 views
0

我们可以根据需要扩展位置弹出menut ..如何显示命令栏菜单在UWP C#中的所需位置?

FrameworkElement senderElement = sender as FrameworkElement; 
myFlyout.ShowAt(sender as UIElement, e.GetPosition(sender as UIElement)); 

但是,如何在需要的位置展开命令栏菜单?任何解决方法? IsOpen propery仅在应用程序的默认右/左/顶部打开命令行!我想打开它靠近我想要的控制/位置。

回答

1

CommandBar不提供ShowAt方法来显示相对于指定元素放置的命令栏菜单。

如果你想扩大在需要的位置命令栏菜单中,你应该能够把AppBarButtonFlyout,而不是把AppBarButtonCommandBar.SecondaryCommands

您可以添加Opening事件CommandBar和使用ShowAt方法显示事件中的Flyout。当您将IsOpen配准CommandBar设置为true时,将触发Opening事件。

例如:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <CommandBar x:Name="MyCommandBar" Width="800" HorizontalAlignment="Center" VerticalAlignment="Center" Opening="MyCommandBar_Opening"> 
     <CommandBar.Resources> 
      <Flyout x:Name="MyBtnFly" Placement="Right"> 
       <StackPanel> 
        <AppBarButton x:Name="CommandBarAppBarButton" Icon="Like" Label="Like" /> 
        <AppBarButton Icon="Dislike" Label="Dislike" /> 
       </StackPanel> 
      </Flyout> 
     </CommandBar.Resources> 
     <AppBarToggleButton Icon="Shuffle" Label="Shuffle" /> 
     <AppBarToggleButton Icon="RepeatAll" Label="Repeat" /> 
     <AppBarSeparator/> 
     <AppBarButton Icon="Back" Label="Back" /> 
     <AppBarButton Icon="Stop" Label="Stop" /> 
     <AppBarButton Icon="Play" Label="Play" /> 
     <AppBarButton Icon="Forward" Label="Forward" /> 
     <CommandBar.Content> 
      <TextBlock Text="Now playing..." Margin="12,14"/> 
     </CommandBar.Content> 
    </CommandBar> 
    <TextBlock x:Name="MyText" Tapped="Button_Click" HorizontalAlignment="Center" VerticalAlignment="Bottom" Text="Click"></TextBlock> 
</Grid> 

代码后面:

private void Button_Click(object sender, TappedRoutedEventArgs e) 
{ 
    MyCommandBar.IsOpen = true; 
} 

private void MyCommandBar_Opening(object sender, object e) 
{ 
    FrameworkElement senderElement = MyText as FrameworkElement; 
    MyBtnFly.ShowAt(senderElement); 
} 
相关问题