Anonymous (owner)

Revisions

  • b9b292 Wed Sep 30 03:38:14 -0700 2009
gist: 197979 Download_button fork
public
Public Clone URL: git://gist.github.com/197979.git
Embed All Files: show embed
VisualStateHelper.cs #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    public class VisualStateHelper : DependencyObject
    {
        public static string GetVisualStateProperty(DependencyObject obj)
        {
            return (string)obj.GetValue(VisualStatePropertyProperty);
        }
 
        public static void SetVisualStateProperty(DependencyObject obj, string value)
        {
            obj.SetValue(VisualStatePropertyProperty, value);
        }
 
        public static readonly DependencyProperty VisualStatePropertyProperty =
            DependencyProperty.RegisterAttached(
                "VisualStateProperty",
                typeof(string),
                typeof(VisualStateHelper),
                new PropertyMetadata((s, e) =>
                {
                    var propertyName = (string)e.NewValue;
                    var ctrl = s as Control;
                    if (ctrl == null)
                        throw new InvalidOperationException(
                            "This attached property only supports types derived from Control.");
                    ctrl.SetBinding(CurrentViewStateProperty, new Binding(propertyName));
                }));
 
        private static readonly DependencyProperty CurrentViewStateProperty =
            DependencyProperty.RegisterAttached(
                "CurrentViewState",
                typeof(string),
                typeof(VisualStateHelper),
                new PropertyMetadata((s,e) =>
                {
                    var ctrl = s as Control;
                    if(ctrl == null)
                        throw new InvalidOperationException("This attached property only supports types derived from Control.");
                    VisualStateManager.GoToState(ctrl, (string)e.NewValue, true);
                }));
    }