Skip to content

Commit

Permalink
update: support any control to attach flyout
Browse files Browse the repository at this point in the history
  • Loading branch information
emako committed Aug 15, 2024
1 parent 152b376 commit 8200eac
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
44 changes: 30 additions & 14 deletions src/Wpf.Ui.Test/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -405,25 +405,41 @@
FontSize="14"
Foreground="{DynamicResource TextFillColorTertiaryBrush}"
Text="Bottom" />
<ui:Button Margin="16,0,0,0" Content="Show Flyout">
<ui:Button Margin="16,0,0,0" Content="Button">
<ui:FlyoutService.Flyout>
<ui:Flyout Placement="Bottom">
<ui:Card>
<StackPanel>
<TextBlock
Width="280"
HorizontalAlignment="Left"
Text="Replicants like any other machine are either a benefit or a hazard. If they're a benefit it's not my problem."
TextWrapping="WrapWithOverflow" />
<Button
Margin="0,8,0,0"
Command="{Binding ShowFlyoutInlineCommand}"
Content="The cake is a lie!" />
</StackPanel>
</ui:Card>
<StackPanel>
<TextBlock
Width="280"
HorizontalAlignment="Left"
Text="Replicants like any other machine are either a benefit or a hazard. If they're a benefit it's not my problem."
TextWrapping="WrapWithOverflow" />
<Button
Margin="0,8,0,0"
Command="{Binding ShowFlyoutInlineCommand}"
Content="The cake is a lie!" />
</StackPanel>
</ui:Flyout>
</ui:FlyoutService.Flyout>
</ui:Button>
<ui:Card Margin="16,0,0,0" Padding="12,8,12,8">
<ui:FlyoutService.Flyout>
<ui:Flyout Placement="Bottom">
<StackPanel>
<TextBlock
Width="280"
HorizontalAlignment="Left"
Text="Replicants like any other machine are either a benefit or a hazard. If they're a benefit it's not my problem."
TextWrapping="WrapWithOverflow" />
<Button
Margin="0,8,0,0"
Command="{Binding ShowFlyoutInlineCommand}"
Content="The cake is a lie!" />
</StackPanel>
</ui:Flyout>
</ui:FlyoutService.Flyout>
<TextBlock Text="Card" />
</ui:Card>
</StackPanel>
</StackPanel>
<StackPanel>
Expand Down
12 changes: 8 additions & 4 deletions src/Wpf.Ui.Violeta/Controls/Flyout/FlyoutService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public static void OnFlyoutChanged(DependencyObject d, DependencyPropertyChanged
}
}
{
buttonExpected.MouseLeftButtonDown -= ShowFlyoutRequested;
buttonExpected.MouseLeftButtonDown += ShowFlyoutRequested;
buttonExpected.MouseLeftButtonUp -= ShowFlyoutRequested;
buttonExpected.MouseLeftButtonUp += ShowFlyoutRequested;
}
}
}
Expand Down Expand Up @@ -91,6 +91,7 @@ public static void ShowFlyout(FrameworkElement buttonExpected)
{
if (GetFlyout(buttonExpected) is Flyout flyout)
{
// Get the flyout popup from Flyout control default template.
flyout.ApplyTemplate();
if (flyout.GetTemplateChild("PART_Popup") is System.Windows.Controls.Primitives.Popup popup)
{
Expand All @@ -107,10 +108,12 @@ public static void ShowFlyout(FrameworkElement buttonExpected)
parent.Children.Remove(popup);
}

// The flyout template will be lost after following code.
// Following code is based on the Flyout control default template.
// If default template is changed, this code will not work.
// Check WPF-UI v3.0.5 since.
if (flyout.Content is not null)
{
(flyout.Content, popup.Child) = (null, flyout.Content as UIElement);
(flyout.Content, ((System.Windows.Controls.Border)popup.Child).Child) = (null, flyout.Content as UIElement);
}

// Spoof the flyout opening state.
Expand All @@ -123,6 +126,7 @@ public static void HideFlyout(FrameworkElement buttonExpected)
{
if (GetFlyout(buttonExpected) is Flyout flyout)
{
// Get the flyout popup from Flyout control default template.
if (flyout.GetTemplateChild("PART_Popup") is System.Windows.Controls.Primitives.Popup popup)
{
// Spoof the flyout opening state.
Expand Down

0 comments on commit 8200eac

Please sign in to comment.