2017-10-19 45 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); 
} 
相關問題