WPF ViewModel get noticed on View Visibility part 2

6 בJanuary 2015

אין תגובות

I was presented a nice trick and solution for case we like the ViewModel get noticed on View

:Loaded , I am talking about this one

.I just forget to show the behavior way to deal with it which is definitely an elegant solution

:You can add simple Behavior like this

public class ViewVisibilityObserverBehavior : Behavior<Window>

 {

   protected override void OnAttached()

    {


    AssociatedObject.DataContextChanged += AssociatedObject_DataContextChanged;

 

    AssociatedObject.Loaded += AssociatedObject_Loaded;

 

    AssociatedObject.IsVisibleChanged += AssociatedObject_IsVisibleChanged;

 

    base.OnAttached();

      }

 

        void AssociatedObject_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)

        {

           // AchievesVIewVisibility(sender);

        }

 

        void AssociatedObject_Loaded(object sender, RoutedEventArgs e)

        {

           // AchievesVIewVisibility(sender);

        }

 

        private void AssociatedObject_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)

        {

          //  AchievesVIewVisibility(sender);

        }

 

        private void AchievesVIewVisibility(object View)

        {

            try

            {

                Window ViewContext = View as Window;

                if (ViewContext.DataContext is IViewVisibileObserver)

                {

                    ViewContext.IsVisibleChanged += (sndr, args) =>

                        {

                            IViewVisibileObserver viewVisibilityObserver = ViewContext.DataContext as IViewVisibileObserver;

                            viewVisibilityObserver.OnVisibilityChanged((bool)args.NewValue);

                        };

                }

            }

            catch (Exception ex)

            {

                Debug.WriteLine("filed to launch behavior");

            }

        }

        protected override void OnDetaching()

        {

            AssociatedObject.DataContextChanged -= AssociatedObject_DataContextChanged;

        }

    }

,I have a purpose why to show some kinds of Events

This is can be very useful in different UI types (aka Window, Control , Native)

.The safest method is the DataContext changed but you can test it by your own on your App

Because you might be surprised from the Events chain.. anyway as reminder we must sets the

:ViewModel as Implementation of this

public interface IViewVisibileObserver

  {

      void OnVisibilityChanged(bool visiblyuStatus);

      bool ViewVisibility { get; }

  }

 

  public class MainviewModel : INotifyPropertyChanged, IViewVisibileObserver

  {

      private bool localviewVisibilityObserver;

 

      public event PropertyChangedEventHandler PropertyChanged;

 

      //ctor

      public MainviewModel()

      {

      }

 

      // IViewVisibileObserver implementation

      public void OnVisibilityChanged(bool visiblyuStatus)

      {

          localviewVisibilityObserver = visiblyuStatus;

      }

 

      public bool ViewVisibility

      {

          get { return localviewVisibilityObserver; }

      }

  }

:The only thin has left is the Xaml connection

<Window x:Class="WpfApplication1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

        xmlns:local="clr-namespace:WpfApplication1"

        Title="MainWindow" Height="350" Width="525">


<i:Interaction.Behaviors>

  <local:ViewVisibilityObserverBehavior></local:ViewVisibilityObserverBehavior> </i:Interaction.Behaviors>

הוסף תגובה
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *