2010-02-05 91 views

回答

4

不,但您可以使用表達行爲來監視您的ViewModel並相應地更改狀態;退房http://blois.us/blog/2009_04_11_houseomirrors_archive.html

+1

的聯繫是現在死了(博客清空)。 – jv42 2011-11-17 16:30:44

+0

但它可以被發現存檔:http://web.archive.org/web/20090417002704/http://blois.us/blog/ – jv42 2011-11-17 16:32:30

1

的名字asigning可視狀態的另一種方式,而對其他類的任何依賴性:

/// <summary> 
    /// Sets VisualState on a control usign an attached dependency property. 
    /// This is useful for a MVVM pattern when you don't want to use imperative 
    /// code on the View. 
    /// </summary> 
    /// <example> 
    /// &lt;TextBlock alloy:VisualStateSetter.VisualStateName=&quot;{Binding VisualStateName}&quot;/&gt; 
    /// </example> 
    public class VisualStateSetter : DependencyObject 
    { 

    /// <summary> 
    /// Gets the name of the VisualState applied to an object. 
    /// </summary> 
    /// <param name="target">The object the VisualState is applied to.</param> 
    /// <returns>The name of the VisualState.</returns> 
    public static string GetVisualStateName(DependencyObject target) 
    { 
     return (string)target.GetValue(VisualStateNameProperty); 
    } 

    /// <summary> 
    /// Sets the name of the VisualState applied to an object. 
    /// </summary> 
    /// <param name="target">The object the VisualState is applied to.</param> 
    /// <param name="visualStateName">The name of the VisualState.</param> 
    public static void SetVisualStateName(DependencyObject target, string visualStateName) 
    { 
     target.SetValue(VisualStateNameProperty, visualStateName); 
    } 

    /// <summary> 
    /// Attached dependency property that sets the VisualState on any Control. 
    /// </summary> 
    public static readonly DependencyProperty VisualStateNameProperty = 
     DependencyProperty.RegisterAttached(
     "VisualStateName", 
     typeof(string), 
     typeof(VisualStateSetter), 
     new PropertyMetadata(VisualStateNameChanged)); 

    /// <summary> 
    /// Callback for the event that the value of the VisualStateProperty changes. 
    /// </summary> 
    /// <param name="sender">The object the VisualState is applied to.</param> 
    /// <param name="args">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param> 
    public static void VisualStateNameChanged(object sender, DependencyPropertyChangedEventArgs args) 
    { 
     string visualStateName = (string)args.NewValue; 
     Control control = sender as Control; 
     if(control == null) 
     { 
     throw new InvalidOperationException("This attached property only supports types derived from Control."); 
     } 
     // Apply the visual state. 
     VisualStateManager.GoToState(control, visualStateName, true); 
    } 
    }