Created
August 1, 2021 09:04
-
-
Save tor4kichi/586f23b4c60541bf8457247735f7345c to your computer and use it in GitHub Desktop.
【UWP】フライアウトの子要素のDataContextにクリックした位置のUI要素のContent/DataContextを突っ込むカスタム添付プロパティ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using Windows.UI.Xaml; | |
| using Windows.UI.Xaml.Controls; | |
| using Windows.UI.Xaml.Controls.Primitives; | |
| namespace NicoVideoSnapshotSearchAssistanceTools.Presentation.Views.Interactions | |
| { | |
| public partial class FlyoutExtensions : DependencyObject | |
| { | |
| public static readonly DependencyProperty DataContextPropagationToFlyoutChildProperty = | |
| DependencyProperty.RegisterAttached( | |
| "DataContextPropagationToFlyoutChild", | |
| typeof(Boolean), | |
| typeof(FlyoutExtensions), | |
| new PropertyMetadata(false, OnDataContextPropagationToFlyoutChildChanged) | |
| ); | |
| private static void OnDataContextPropagationToFlyoutChildChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | |
| { | |
| if (e.NewValue is bool b && b == true) | |
| { | |
| var flyout = d as FlyoutBase; | |
| flyout.Opening += MenuFlyout_Opening; | |
| } | |
| } | |
| private static void MenuFlyout_Opening(object sender, object e) | |
| { | |
| var flyout = sender as FlyoutBase; | |
| var dataContext = (flyout.Target as SelectorItem)?.Content ?? flyout.Target.DataContext; | |
| if (dataContext == null) | |
| { | |
| throw new InvalidOperationException(); | |
| } | |
| if (flyout is MenuFlyout menuFlyout) | |
| { | |
| foreach (var menuItem in menuFlyout.Items) | |
| { | |
| menuItem.DataContext = dataContext; | |
| } | |
| } | |
| else if (flyout is Flyout simpleFlyout) | |
| { | |
| if (simpleFlyout.Content is FrameworkElement fe) | |
| { | |
| fe.DataContext = dataContext; | |
| } | |
| } | |
| else if (flyout is CommandBarFlyout cmdBarFlyout) | |
| { | |
| foreach (var item in cmdBarFlyout.PrimaryCommands) | |
| { | |
| if (item is FrameworkElement fe) | |
| { | |
| fe.DataContext = dataContext; | |
| } | |
| } | |
| foreach (var item in cmdBarFlyout.SecondaryCommands) | |
| { | |
| if (item is FrameworkElement fe) | |
| { | |
| fe.DataContext = dataContext; | |
| } | |
| } | |
| } | |
| } | |
| public static void SetDataContextPropagationToFlyoutChild(FlyoutBase element, Boolean value) | |
| { | |
| element.SetValue(DataContextPropagationToFlyoutChildProperty, value); | |
| } | |
| public static Boolean GetDataContextPropagationToFlyoutChild(FlyoutBase element) | |
| { | |
| return (Boolean)element.GetValue(DataContextPropagationToFlyoutChildProperty); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment