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);
}
}
}
, .