Where to access DataContext in WinRT XAML UserControl

I have UserControlViewModelone that raises an event:

public event EventHandler<EventArgs> StuffDone;

An object UserControlViewModelis created and initialized internally MainPageViewModel:

this.userControlViewModel = new UserControlViewModel();

MainPageViewModelis a View model for MainPage.

In MainPage.xaml, I have the following code to place UserControlView UserControlin MainPageand initialize it DataContext:

<views:UserControlView DataContext="{Binding userControlViewModel, Mode=OneWay}" IsHitTestVisible="False"></views:UserControlView>

So far, everything is working fine.

Now I want to subscribe to the StuffDoneevent inside UserControlView. The first thing that occurred to me was to do this inside Loadedan event handler UserControlView; however, DataContextat this point still null. Scanning the rest of the events UserControldid not give me any clue.

So where is the right place to receive DataContextand subscribe to his events?

.

+5
1

UPDATE: DataContextChanged WinRT Windows 8.1. , WinRT Windows 8 , DataContextChanged.

, , , Will , .

, :

IDataContextChangedHandler.Generic.cs:

using Windows.UI.Xaml;

namespace SomeNamespace
{
    public interface IDataContextChangedHandler<in T> where T : FrameworkElement
    {
        void DataContextChanged(T sender, DependencyPropertyChangedEventArgs e);
    }
}

DataContextChangedHelper.Generic.cs:

using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;

namespace SomeNamespace
{
    public sealed class DataContextChangedHandler<T> where T : FrameworkElement, IDataContextChangedHandler<T>
    {
        private readonly DependencyProperty internalDataContextProperty =
            DependencyProperty.Register(
                "InternalDataContext",
                typeof(object),
                typeof(T),
                new PropertyMetadata(null, DataContextChanged));

        private static void DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var control = sender as T;

            if (control == null) { return; }

            control.DataContextChanged(control, e);
        }

        public void Bind(T control)
        {
            control.SetBinding(this.internalDataContextProperty, new Binding());
        }
    }
}

UserControlView.xaml.cs:

using Windows.UI.Xaml;

namespace SomeNamespace
{
    public sealed partial class UserControlView : IDataContextChangedHandler<UserControlView>
    {
        private readonly DataContextChangedHandler<UserControlView> handler = new DataContextChangedHandler<UserControlView>();

        public UserControlView()
        {
            this.InitializeComponent();

            this.handler.Bind(this);
        }

        public void DataContextChanged(UserControlView sender, DependencyPropertyChangedEventArgs e)
        {
            var viewModel = e.NewValue as UserControlViewModel;

            if (viewModel == null) { return; }

            viewModel.SomeEventRaised += (o, args) => VisualStateManager.GoToState(this, "TheOtherState", false);
        }
    }
}

, .

+1

All Articles