Visualstatemanager и привязка данных - PullRequest
1 голос
/ 05 февраля 2010

Могу ли я связать состояние визуального менеджера состояний с моей моделью представления?

Ответы [ 2 ]

4 голосов
/ 05 февраля 2010

Нет, но вы можете использовать Expression Behavior, который следит за вашей ViewModel и соответственно изменяет состояния; проверить http://blois.us/blog/2009_04_11_houseomirrors_archive.html

1 голос
/ 08 февраля 2010

Другой способ присвоения визуальных состояний по имени, без какой-либо зависимости от других классов:

  /// <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 );
    }
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...