Created
November 15, 2012 13:04
-
-
Save Nilzor/4078545 to your computer and use it in GitHub Desktop.
StateHelper
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.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using GalaSoft.MvvmLight.Messaging; | |
using GTWin8.Messages; | |
using GTWin8.Ui; | |
using Windows.UI.Xaml; | |
using Windows.UI.Xaml.Controls; | |
namespace MvvmHelpers | |
{ | |
/// <summary> | |
/// Copied from http://stackoverflow.com/questions/6002046/binding-visualstatemanager-view-state-to-a-mvvm-viewmodel | |
/// </summary> | |
public class StateHelper : DependencyObject | |
{ | |
public static readonly DependencyProperty StateProperty = DependencyProperty.RegisterAttached( | |
"State", typeof(String), typeof(StateHelper), new PropertyMetadata("Normal", StateChanged)); | |
internal static void StateChanged(DependencyObject target, DependencyPropertyChangedEventArgs args) | |
{ | |
try | |
{ | |
string newState = (string)args.NewValue; | |
Debug.WriteLine(String.Format("Attempting change from {0} to {1} on {2} ",args.OldValue, newState, target)); | |
if (args.NewValue != null) | |
{ | |
bool res = VisualStateManager.GoToState((Control)target, newState, true); | |
Messenger.Default.Send<StateChangedMessage>(new StateChangedMessage { Control = (Control)target, NewState = newState }); | |
Debug.WriteLine("Result: " + res); | |
} | |
} | |
catch (Exception ex) | |
{ | |
System.Diagnostics.Debug.WriteLine("StateHelper: " + ex.Message); | |
} | |
} | |
public static void SetState(DependencyObject obj, string value) | |
{ | |
obj.SetValue(StateProperty, value); | |
} | |
public static string GetState(DependencyObject obj) | |
{ | |
return (string)obj.GetValue(StateProperty); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please answer my qestion on StackOverflow
http://stackoverflow.com/questions/14938037/statehelper-visualstatemanager-with-mvvm