From fb2385041c8cb6061ce1eefe48e5bf3085f90c64 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sat, 12 Oct 2019 19:26:32 -0500 Subject: [PATCH] add VisualState template/proxy dependency props to support visual state shared style resources --- src/client/spark/view/Utility/StateManager.cs | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/client/spark/view/Utility/StateManager.cs b/src/client/spark/view/Utility/StateManager.cs index 76b04e6e3..25030d899 100644 --- a/src/client/spark/view/Utility/StateManager.cs +++ b/src/client/spark/view/Utility/StateManager.cs @@ -4,6 +4,7 @@ using mbts.ui.extensions; using mbts.ui.shell; using mbts.ui.utility; using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Reactive.Disposables; @@ -232,7 +233,7 @@ namespace mbts.ui { } [ContentProperty(nameof(StateDefinitions))] - class StateGroupsManager : DependencyObject { + class StateGroupsManager : FrameworkElement { IDisposable _state_definitions_hook; public bool OverrideTarget { get; set; } @@ -305,6 +306,36 @@ namespace mbts.ui { // // class StateManager : DependencyObject { + // This is workaround to name VisualStates/Groups in a ResourceDictionary.xaml file. + public static readonly DependencyProperty NameProperty = + DependencyProperty.RegisterAttached("Name", typeof(string), typeof(StateManager), + new PropertyMetadata(OnNameChanged)); + + static void OnNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { + if (d is VisualState state) + state.Name = e.NewValue?.ToString(); + else if (d is VisualStateGroup group) + group.Name = e.NewValue?.ToString(); + } + + public static string GetName(DependencyObject d) => d.GetValue(NameProperty) as string; + public static void SetName(DependencyObject d, string value) => d.SetValue(NameProperty, value); + + public static readonly DependencyProperty VisualStateGroupsProxyProperty = + DependencyProperty.RegisterAttached("VisualStateGroupsProxy", typeof(IEnumerable), typeof(StateManager), new PropertyMetadata(OnVisualStateGroupsProxyChanged)); + + static void OnVisualStateGroupsProxyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { + var el = (FrameworkElement)d; + var groups = VisualStateManager.GetVisualStateGroups(el); + groups.Clear(); + if (e.NewValue is IEnumerable new_state_groups) { + groups.AddRange(new_state_groups); + } + } + + public static IEnumerable GetVisualStateGroupsProxy(DependencyObject d) => d.GetValue(VisualStateGroupsProxyProperty) as IEnumerable; + public static void SetVisualStateGroupsProxy(DependencyObject d, IEnumerable value) => d.SetValue(VisualStateGroupsProxyProperty, value); + #region Groups public static readonly DependencyProperty GroupsProperty = DependencyProperty.RegisterAttached("Groups", typeof(StateGroupsManager), typeof(StateManager), new PropertyMetadata((d, e) => { @@ -316,6 +347,23 @@ namespace mbts.ui { public static void SetGroups(DependencyObject d, StateGroupsManager value) => d.SetValue(GroupsProperty, value); #endregion + #region GroupsTemplate + public static readonly DependencyProperty GroupsTemplateProperty = + DependencyProperty.RegisterAttached("GroupsTemplate", typeof(DataTemplate), typeof(StateManager), new PropertyMetadata(OnGroupsTemplateChanged)); + + static void OnGroupsTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { + if (e.NewValue is DataTemplate dt) { + var target = (FrameworkElement)d; + var content = dt.LoadContent(); + if (content is StateGroupsManager sgm) + target.SetCurrentValue(GroupsProperty, sgm); + } + } + + public static DataTemplate GetGroupsTemplate(DependencyObject d) => d.GetValue(GroupsTemplateProperty) as DataTemplate; + public static void SetGroupsTemplate(DependencyObject d, object value) => d.SetValue(GroupsTemplateProperty, value); + #endregion + public static string GetVisualState(DependencyObject obj) { return (string)obj.GetValue(VisualStateProperty); }