How to attach Flyout to MenuFlyoutItem? - xaml

XAML code:
<Page.TopAppBar>
<CommandBar x:Name="bottomAppBar" Padding="10,0,10,0">
<AppBarButton AutomationProperties.Name="Sample Button"
AutomationProperties.AutomationId="SampleAppBarButton"
Click="AppBarButton_Click">
<AppBarButton.Flyout>
<MenuFlyout>
<MenuFlyoutItem x:Name="MuteMenu" Icon="Mute" Text="Mute" Click="MuteMenu_Click">
<FlyoutBase.AttachedFlyout>
<Flyout>
<TextBlock Text="Some text..."/>
</Flyout>
</FlyoutBase.AttachedFlyout>
</MenuFlyoutItem>
</MenuFlyout>
</AppBarButton.Flyout>
</AppBarButton>
</CommandBar>
</Page.TopAppBar>
C++/CX:
void App2::DirectXPage::MuteMenu_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
FlyoutBase::ShowAttachedFlyout((FrameworkElement^)sender);
}
But ShowAttachedFlyout not working - flyout is not appearing when I click menu item. No errors reported.
Creating and attaching flyout programmatically doesn't work as well.
Target version is 10.0.18362.0.
Visual Studio 2019 (v142).

flyout is not appearing when I click menu item
By testing your code, the flyout doesn't appear is because after the MenuFlyoutItem is clicked, the entire MenuFlyout will be hidden, the Flyout inside it can't appear.
You can try to use MenuFlyoutSubItem which contains a cascading list of menu items.
<Page.TopAppBar>
<CommandBar x:Name="bottomAppBar" Padding="10,0,10,0">
<AppBarButton AutomationProperties.Name="Sample Button"
AutomationProperties.AutomationId="SampleAppBarButton"
x:Name="MyAppButton" >
<AppBarButton.Flyout>
<MenuFlyout>
<MenuFlyoutSubItem x:Name="MuteMenu" Icon="Mute" Text="Mute">
<MenuFlyoutItem Text="Some Text..."></MenuFlyoutItem>
<MenuFlyoutItem Text="123"></MenuFlyoutItem>
</MenuFlyoutSubItem>
</MenuFlyout>
</AppBarButton.Flyout>
</AppBarButton>
</CommandBar>
</Page.TopAppBar>
Or you can add a Button.Flyout and include the menuFlyout in the Button.Flyout.
<Page.TopAppBar>
<CommandBar x:Name="bottomAppBar" Padding="10,0,10,0">
<AppBarButton x:Name="button" >
<AppBarButton.Flyout>
<Flyout>
<StackPanel>
<Button>
<StackPanel Orientation="Horizontal">
<FontIcon FontFamily="Segoe MDL2 Assets" Glyph=""></FontIcon>
<TextBlock>Mute</TextBlock>
</StackPanel>
<Button.Flyout>
<MenuFlyout>
<MenuFlyoutItem Text="Some text..."></MenuFlyoutItem>
</MenuFlyout>
</Button.Flyout>
</Button>
</StackPanel>
</Flyout>
</AppBarButton.Flyout>
</CommandBar>
</Page.TopAppBar>

Related

Is it possible have Flyout menu over MapControl on UWP application?

Is it any idea to create menu over MapControl on UWP like below image ?
There are multiple ways to do it. According to your comments, one way may be to put dummy UIElement under the map (with suitable menu) and show it when needed. XAML:
<Grid x:Name="MainGrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Border x:Name="SamapleBorder" HorizontalAlignment="Left" VerticalAlignment="Top">
<Border.ContextFlyout>
<MenuFlyout Placement="Bottom">
<MenuFlyoutItem Text="Item 1"/>
<MenuFlyoutItem Text="Item 2"/>
<MenuFlyoutItem Text="Item 3"/>
</MenuFlyout>
</Border.ContextFlyout>
</Border>
<Rectangle Fill="Beige" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Tapped="Rectangle_Tapped"/>
</Grid>
and the code behind:
private void Rectangle_Tapped(object sender, TappedRoutedEventArgs e) => this.SamapleBorder.ContextFlyout.ShowAt(this.SamapleBorder);

click event of appbar button within a flyout not working in UWP

I have a commandbar and on one of the appbar buttons I attached a flyout which shows up properly. This flyout again has a command bar with appbar buttons. The problem is that I wanted to open a flyout again on one of these buttons but that does not work. Please help!
<Page.Resources>
<Flyout x:Key="SetBookmarkFlyout">
<Grid Width="250" Height="250">
<TextBlock Grid.Row="0">Name</TextBlock>
<TextBox Grid.Row="1" x:Name="BookmarkName"></TextBox>
<TextBlock Grid.Row="2">Create in</TextBlock>
<ComboBox Grid.Row="3"></ComboBox>
</Grid>
</Flyout>
<Flyout x:Key="SubMenuFlyout">
<CommandBar Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Height="250" >
<AppBarButton Icon="Bookmarks" ToolTipService.ToolTip="Open Bookmarks"></AppBarButton>
<AppBarButton x:Name="SetBookmark" Icon="Favorite" ToolTipService.ToolTip="Set Bookmark" Click="SetBookmark_Click" FlyoutBase.AttachedFlyout="{StaticResource SetBookmarkFlyout}" ></AppBarButton>
<AppBarButton Icon="Page" ToolTipService.ToolTip="Recent Files"></AppBarButton>
</CommandBar>
</Flyout>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Padding="0,0,0,-10" >
<CommandBar x:Name="Commandbar" Height="70" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<AppBarButton Icon="Back" Label="" ToolTipService.ToolTip="Back" ></AppBarButton>
<AppBarButton Icon="Forward" Label="" ToolTipService.ToolTip="Forward" ></AppBarButton>
<AppBarButton x:Name="SpaceAppBtn"></AppBarButton>
<AppBarButton Icon="Add" Label="" ToolTipService.ToolTip="Add Tab"></AppBarButton>
<AppBarButton Icon="Favorite" Label="" ToolTipService.ToolTip="Favorites (Set Bookmark,open Bookmarks, and see recent files)" Click="AppBarButton_Click" FlyoutBase.AttachedFlyout="{StaticResource SubMenuFlyout}">
</AppBarButton>
<AppBarButton Icon="Find" Label="" ToolTipService.ToolTip="Search"></AppBarButton>
</CommandBar>
</Grid>
I have added your Flyout directly to the Button in my example to test.
<AppBarButton x:Name="FavoriteButton" Icon="Favorite" Label="" ToolTipService.ToolTip="Favorites (Set Bookmark,open Bookmarks, and see recent files)"
Click="AppBarButton_Click">
<AppBarButton.Flyout>
<Flyout>
<CommandBar Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Height="250" >
<AppBarButton Icon="Bookmarks" ToolTipService.ToolTip="Open Bookmarks"></AppBarButton>
<AppBarButton x:Name="SetBookmark" Icon="Favorite" ToolTipService.ToolTip="Set Bookmark"
Click="SetBookmark_Cick">
</AppBarButton>
<AppBarButton Icon="Page" ToolTipService.ToolTip="Recent Files"></AppBarButton>
</CommandBar>
</Flyout>
</AppBarButton.Flyout>
</AppBarButton>
Use a Popup rather than the second Flyout.
After CommandBar add
<CommandBar>...
</CommandBar>
<Popup x:Name="SetBookmarkPopup" HorizontalOffset="200" VerticalOffset="200">
<Grid Width="250" Height="250">
<TextBlock Grid.Row="0">Name</TextBlock>
<TextBox Grid.Row="1" x:Name="BookmarkName"></TextBox>
<TextBlock Grid.Row="2">Create in</TextBlock>
<ComboBox Grid.Row="3"></ComboBox>
</Grid>
</Popup>
and call from your AppBarButton_Click event
private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
FavoriteButton.Flyout.ShowAt(FavoriteButton);
}
finally open the Popup
private void SetBookmark_Cick(object sender, RoutedEventArgs e)
{
SetBookmarkPopup.IsOpen = true;
}
Hope that helps

Adding ListviewItem in Splitview

I am creating a Windows 10 universal App and I Have successfully created shell.xaml but I don't wanna use radioButton instead of that I have used a button and TextBlock.
I want to know how to make the TextBlock and Button a Single clickable entity through listView Item.
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<SplitView x:Name="mySplitView" DisplayMode="CompactInline" IsPaneOpen="False"
CompactPaneLength="50" OpenPaneLength="150" Content="{Binding}">
<SplitView.Pane>
<StackPanel Background="{ThemeResource SystemControlBackgroundAccentBrush}">
<Button x:Name="HamburgerButton" FontFamily="Segoe MDL2 Assets" Content=""
Width="50" Height="50" Background="Transparent" Foreground="White" Click="HamburgerButton_Click" />
<StackPanel Orientation="Horizontal">
<Button FontFamily="Segoe MDL2 Assets" Content=""
Width="50" Height="50" Background="Transparent" Foreground="White">
<TextBlock Foreground="White" FontSize="10" VerticalAlignment="Center" />
</StackPanel>
From what I understand you want to make an event fire whenever you press somewhere inside the StackPanel that contains the Button and TextBlock.
One solution would be to simply put your Button and TextBlock inside a ListView item.
Like this (I include all the xaml for the SplitView.Pane to make it a bit more clear):
<SplitView.Pane>
<StackPanel Background="{ThemeResource SystemControlBackgroundAccentBrush}">
<Button x:Name="HamburgerButton" FontFamily="Segoe MDL2 Assets" Content="" Width="50" Height="50" Background="Transparent" Foreground="White" Click="HamburgerButton_Click" />
<ListView>
<ListView.Items>
<ListViewItem Padding="0" Tapped="ListViewItem_Tapped">
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Height="50">
<Button FontFamily="Segoe MDL2 Assets" Content="" Width="50" Height="50" Background="Transparent" Foreground="White" />
<TextBlock Foreground="White" FontSize="10" Text="Wop wop!" VerticalAlignment="Center" />
</StackPanel>
</ListViewItem>
</ListView.Items>
</ListView>
</StackPanel>
</SplitView.Pane>
In this example only the '%' is shown when the pane is closed and the '%' plus the text "Wop wop!" when it's open. Whenever the content of this ListViewItem (the buton or TextBlock) is pressed, the method "ListViewItem_Tapped" will fire.
If I in any way misunderstood your question or you need any more info about my answer please let me know.
Have a wonderful day!
PS. The button still "acts" like a button, by showing it's own border, visual states and so on. I don't know from the top of my head how to disable this. But you could perhaps try disabling it or using another textblock instead? .DS

WinRT XAML Cancel Event on Element on Top of Another

I have a square that has a Tapped event handler. Inside the square (on top of) is another button that has a Flyout. When I click the button to open the Flyout, the event is also fired for the square.
How can I make it so the event isn't fired on the square if the Flyout was clicked?
How can I cancel Handled = true a RoutedEvent that happens through a Command?
Here is the XAML
<Grid Margin="0, 0, 50, 50" Width="300">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<core:InvokeCommandAction Command="{Binding DataContext.MyTapCommand, ElementName=LayoutRoot}" CommandParameter="{Binding}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
<Image Source="{Binding MyImagePath}" Width="300" />
<Border Background="#aa000000" HorizontalAlignment="Stretch" VerticalAlignment="Bottom">
<TextBlock Text="{Binding Title}" Margin="5" FontSize="18" TextWrapping="Wrap" />
</Border>
<Button VerticalAlignment="Top" HorizontalAlignment="Right">
<Button.Template>
<ControlTemplate>
<Grid>
<Canvas Height="50" Width="50">
<Polygon Points="0,0 50,0 50,50 0,0" Fill="Gray" />
<TextBlock Text="+" FontSize="40" Canvas.Top="-16" Canvas.Left="22" Padding="0" />
</Canvas>
</Grid>
</ControlTemplate>
</Button.Template>
<Button.Flyout>
<MenuFlyout>
<MenuFlyoutItem Text="Item 1" Command="{Binding DataContext.Item1Command, ElementName=LayoutRoot}" CommandParameter="{Binding}" />
<MenuFlyoutItem Text="Item 2" Command="{Binding DataContext.Item2Command, ElementName=LayoutRoot}" CommandParameter="{Binding}" />
</MenuFlyout>
</Button.Flyout>
</Button>
</Grid>
So there is an image that is the whole item. You can click the image and a command should fire. There is a button that sits on top of the image. If the button is clicked, I don't want the Tapped event to fire.
I was able to just put a Tapped handler on the button that contains the Flyout and do e.Handled = true. The Flyout still opens but the second event doesn't happen.
i can't reproduce the mentioned issue, anyway i guess your issue can be resolved by the below code snippet,
//Button tapped event
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if ((sender) is Button)
{
// your code
}
}
Therefore when the sender is square it can be omitted.
I hope it helps you.
Thanks,
Joy Oyiess Rex K

Windows Phone 8 Nokia Maps: How to change the design of a Microsoft.Phone.Maps.Toolkit Pushpin?

Default, they look like this: http://wp.qmatteoq.com/wp-content/uploads/2013/01/map.png. I would like to have them look like on Nokia Maps, like this: http://www.themobileindian.com/images/nnews/2012/11/9225/Nokia-Maps.jpg, so they take less space. And everytime I tap on them, they will toggle between icon and description.
Lets say I have two templates for pushpin in resources:
<ControlTemplate x:Key="1" TargetType="maptk:Pushpin">
<Grid x:Name="ContentGrid" Background="Transparent" Margin="-4,0,0,0">
<StackPanel >
<Grid Background="Black">
<StackPanel Margin="5,5,0,0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<cmd:EventToCommand PassEventArgsToCommand="False"
CommandParameter="{Binding}"
Command="{Binding ElementName=NearbyMap, Path=DataContext.Pushpin_OnTapCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<TextBlock Text="{Binding Location}" Foreground="White" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding LocationName}" Foreground="White" />
<TextBlock Text="-" Foreground="White" Padding="3,0"/>
<TextBlock Text="{Binding LocationName}" Foreground="White" />
</StackPanel>
<TextBlock Text="{Binding LocationName}" Foreground="White" />
<TextBlock Text="{Binding LocationName}" Foreground="White" />
</StackPanel>
</Grid>
<Polygon Fill="Black" Points="0,0 29,0 0,29" Width="29" Height="29" HorizontalAlignment="Left" />
<Grid Height="26" Width="26" Margin="-13,-13,0,0" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Left">
<Grid.RenderTransform>
<CompositeTransform Rotation="-45"/>
</Grid.RenderTransform>
<Rectangle Fill="Black" HorizontalAlignment="Center" Margin="0" Stroke="White" VerticalAlignment="Center" Height="26" Width="26" />
<Ellipse HorizontalAlignment="Center" Height="16" Margin="0" VerticalAlignment="Center" Fill="Green" Width="16" />
</Grid>
</StackPanel>
</Grid>
</ControlTemplate>
<ControlTemplate TargetType="maptk:Pushpin" x:Key="2">
<Grid Height="26" Width="26" Margin="-13,-13,0,0" RenderTransformOrigin="0.5,0.5" >
<Grid.RenderTransform>
<CompositeTransform Rotation="-45"/>
</Grid.RenderTransform>
<Rectangle Fill="Black" HorizontalAlignment="Center" Margin="0" Stroke="White" VerticalAlignment="Center" Height="26" Width="26"/>
<Ellipse HorizontalAlignment="Center" Height="16" Margin="0" VerticalAlignment="Center" Fill="Red" Width="16"/>
</Grid>
</ControlTemplate>
and the pushpin control:
<maptk:Pushpin x:Name="PushPins" GeoCoordinate="{Binding Location}" Visibility="Visible" Content="{Binding LocationName}" Template="{StaticResource 2}"/>
How can I switch between them with some triggers or something?
#Rares found another solution, that works great in my case...
So the general issue here is just, how to switch/change the design when tapping the pushpin.
Well it seems that when you add the pushpins to the map they get their design from the toolkit, but you still are able to change that by setting the Style propery in code!
So if you hook up each pushpin to a pushpin tap event you can just do the following in your code behind of the XAML page:
private void Pushpin_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
((Pushpin)sender).Style = Application.Current.Resources["PushpinStyle"] as Style;
}
Provided you created a PushpinStyle inside a resourcedictionary that has a Pushpin as TargetType.
To be able to reset the design when pushing on another pushpin, I just keep a reference to the pushpin pressed and reset it's style before changing the style of the newly pressed one!
Works great for me...
You can use a general trick to modify the look of TK components. This can be used for nearly all TK components.
All TK components have styles (defined in XAML), these are not settable via code. But you can override the style in your applicatoin (keep in mind, you override the style in general, so if you modify e.g. pushpin styles, all pushpins inside your app will look like that)
A bit more in detail:
a) Get the TK source from https://phone.codeplex.com/sourcecontrol/latest
b) Have a look into Themes/Generic.xaml
c) Next to the last element, you see the style definition for the pushpin.
Starts with:
<!-- Default Style used for Pushpin -->
<Style TargetType="maptk:Pushpin">
...
d) Copy this style (or you can write it from scratch of course) and modify the style to whatever you like
e) Add this modified code snippet to your Application.Resources (e..g. the Application.Resources section in your App.xaml)
Keep in mind, you also need to reference the right TK namespace.
For pushpin, you need to include:
xmlns:maptk="clr-namespace:Microsoft.Phone.Maps.Toolkit"
Now your app uses the locally overriden style for your pushpin.
Btw: Since TK is Ms-PL licensed, keep in mind that there are probably license restrictions if you take the original source code from MSFT.
I personally found it easier to not use the toolkit for pushpins. Instead I wrote a user control "PushPinUC" that I designed how I wanted it. The UserControl subscribes to the Tap event - so I can expand/retract when it's pressed.
You can add user controls to your Map control using the following code.
private void BindPushpins(IEnumerable<PushpinIVM> pushpins)
{
foreach (var pushpin in pushpins)
{
var pushPinUC = new PushpinUC { Pushpin = pushpin };
var mapOverlay = new MapOverlay { GeoCoordinate = pushpin.Location, Content = pushPinUC };
var mapLayer = new MapLayer { mapOverlay };
Map.Layers.Add(mapLayer);
}
}
I found a solution: add only one control template to the pushpins. The stackpanel is the pushpin with the textbox and the grid before the stackpanel is the pushpin without the textbox. The stackpanel is with visibility collapsed. As you can see there are 2 triggers for the tap event, where i just set the visibility of the pushpin with the textbox to visible or collapsed. You can use any image for the pushpins.
<ctrls:BaseUserControl.Resources>
<ControlTemplate x:Key="PushPinTemplate" TargetType="maptk:Pushpin">
<Grid x:Name="ContentGrid" Background="Transparent" Margin="-4,0,0,0">
<Grid Height="40" Width="30" Margin="-13,-13,0,0" RenderTransformOrigin="0.5,0.5" >
<Image Source="/Assets/Images/push1.png" Width="40" Height="40">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<cmd:EventToCommand PassEventArgsToCommand="True"
Command="{Binding ElementName=NearbyMap, Path=DataContext.Rectangle_OnTapCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
</Grid>
<StackPanel x:Name="DetailsPanel" Visibility="{Binding Path=Visibility}">
<Grid x:Name="TestGrid" Background="Black">
<StackPanel Margin="5,5,0,0">
<!--TextBlock tap-->
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<cmd:EventToCommand PassEventArgsToCommand="True"
Command="{Binding ElementName=NearbyMap, Path=DataContext.Pushpin_OnTapCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<TextBlock Text="{Binding LocationName}" Foreground="White" FontSize="25"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding StreetName}" Foreground="White" />
<TextBlock Text="{Binding StreetNumber}" Foreground="White" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding DistanceToDisplay}" Foreground="White" />
</StackPanel>
</StackPanel>
</Grid>
<Polygon Fill="Black" Points="0,0 29,0 0,29" Width="29" Height="29" HorizontalAlignment="Left" Visibility="Visible"/>
<Grid Height="40" Width="30" Margin="-13,-13,0,0" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Left">
<Image Source="/Assets/Images/push2.png" Width="40" Height="40">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<cmd:EventToCommand PassEventArgsToCommand="True"
Command="{Binding ElementName=NearbyMap, Path=DataContext.Rectangle_OnTapCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
</Grid>
</StackPanel>
</Grid>
</ControlTemplate>
</ctrls:BaseUserControl.Resources>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<maps:Map x:Name="NearbyMap"
Center="{Binding MapCenter, Mode=TwoWay}"
ZoomLevel="15"
dp:MapPushPinDependency.ItemsSource="{Binding Path=Locations, Mode=OneWay}"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<cmd:EventToCommand PassEventArgsToCommand="True"
Command="{Binding ElementName=NearbyMap, Path=DataContext.Map_OnTapCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<maptk:MapExtensions.Children>
<maptk:MapItemsControl Name="StoresMapItemsControl">
<maptk:MapItemsControl.ItemTemplate>
<DataTemplate>
<maptk:Pushpin x:Name="PushPins" GeoCoordinate="{Binding Location}" Visibility="Visible" Content="{Binding LocationName}" Template="{StaticResource PushPinTemplate}"/>
</DataTemplate>
</maptk:MapItemsControl.ItemTemplate>
</maptk:MapItemsControl>
<maptk:UserLocationMarker x:Name="UserLocationMarker" Visibility="Visible" GeoCoordinate="{Binding MyLocation}"/>
</maptk:MapExtensions.Children>
</maps:Map>
</Grid>
#Depechie
private void RectangleTapAction(GestureEventArgs e)
{
var pushpinControl = TryFindParent<Pushpin>(e.OriginalSource as UIElement);
var pushpin = (pushpinControl as FrameworkElement).DataContext as PushPinModel;
Locations.Where(t => t.Id != pushpin.Id).ToList().ForEach(t => t.Visibility = Visibility.Collapsed);
pushpin.Visibility = pushpin.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
e.Handled = true;
}
Locations is an ObservableCollection and there is a Linq where I hide the textbox for other pushpins. PushPinModel has an attribute representing the visibility.