Created
February 20, 2014 08:03
-
-
Save Miista/9108881 to your computer and use it in GitHub Desktop.
VisualStateHelper for .NET
This file contains 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; | |
using System.Windows; | |
using System.Windows.Controls.Primitives; | |
using System.Windows.Media; | |
namespace NCore.Helpers { | |
public class VisualStateHelper { | |
public static void HookEvent<T>( DependencyObject onElement, string inVisualStateGroup, EventHandler<VisualStateChangedEventArgs> setHandler) where T : DependencyObject { | |
T button = VisualStateHelper.FindSimpleVisualChild<T>(onElement); | |
if (button != null) { | |
FrameworkElement _element = VisualTreeHelper.GetChild(button, 0) as FrameworkElement; | |
if (onElement != null) { | |
VisualStateGroup group = VisualStateHelper.FindVisualState(_element, inVisualStateGroup); | |
if (group != null) { | |
group.CurrentStateChanging += setHandler; | |
} | |
} | |
} | |
} | |
static void group_CurrentStateChanging( object sender, VisualStateChangedEventArgs e ) { | |
throw new NotImplementedException(); | |
} | |
static VisualStateGroup FindVisualState( FrameworkElement element, string name ) { | |
if (element == null) { | |
return null; | |
} | |
IList groups = VisualStateManager.GetVisualStateGroups(element); | |
foreach (VisualStateGroup group in groups) { | |
if (group.Name == name) { | |
return group; | |
} | |
} | |
return null; | |
} | |
public static T FindSimpleVisualChild<T>( DependencyObject element ) where T : class { | |
while (element != null) { | |
if (element is T) { | |
return element as T; | |
} | |
element = VisualTreeHelper.GetChild(element, 0); | |
} | |
return null; | |
} | |
public static ViewportControl FindViewport(DependencyObject parent) { | |
var childCount = VisualTreeHelper.GetChildrenCount(parent); | |
for (var i = 0; i < childCount; i++) { | |
var elt = VisualTreeHelper.GetChild(parent, i); | |
if (elt is ViewportControl) | |
return (ViewportControl) elt; | |
var result = FindViewport(elt); | |
if (result != null) | |
return result; | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment