add VisualState template/proxy dependency props to support visual state shared style resources

This commit is contained in:
John Colvin committed 2019-10-19 17:29:24 -05:00
1 parent 98685e59b3
commit fb2385041c
1 file changed
+49 -1
+49 -1
View File
@@ -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 {
// </VisualStateGroup>
//</VisualStateManager.VisualStateGroups>
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<VisualStateGroup>), 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<VisualStateGroup> new_state_groups) {
groups.AddRange(new_state_groups);
}
}
public static IEnumerable<VisualStateGroup> GetVisualStateGroupsProxy(DependencyObject d) => d.GetValue(VisualStateGroupsProxyProperty) as IEnumerable<VisualStateGroup>;
public static void SetVisualStateGroupsProxy(DependencyObject d, IEnumerable<VisualStateGroup> 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);
}