2013-03-08 44 views
5

我有UserControlViewModel这引起了一个事件:从哪里进入的DataContext中的WinRT XAML用户控件

public event EventHandler<EventArgs> StuffDone; 

UserControlViewModel一个对象被创建和初始化里面MainPageViewModel

this.userControlViewModel = new UserControlViewModel(); 

MainPageViewModel是查看 - 型号为MainPage

在MainPage.xaml中,我有下面的代码放置UserControlViewUserControlMainPage并初始化其DataContext

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

到目前为止,一切工作正常。

现在我想订阅UserControlView内的StuffDone事件。我发生的第一件事是在Loaded事件处理程序UserControlView内部执行;然而,在那一点的DataContext仍然是null。扫描UserControl事件的其余部分,我根本没有线索。

那么,在哪里可以获得DataContext并订阅其活动?

在此先感谢。

+2

痛苦的。没有事件,并且DataContext依赖项属性上没有重写元数据。回家吧,微软,你喝醉了。这家伙有一个解决方案在这里:http://dotneteers.net/blogs/vbandi/archive/2013/01/23/datacontextchanged-event-for-winrt.aspx哈克。 – Will 2013-03-08 16:39:05

+0

我已经做到了这一点,它对我来说工作得很好。但我用“Mode = TwoWay”。在UserControl的Loaded事件中,我可以访问ViewModel(当然,您必须将DataCotext投射到ViewModel才能访问它) – SachiraChin 2013-03-09 02:09:05

+0

@Sach,完成了什么? [Will](http://stackoverflow.com/users/1228/will)会提出什么建议?如果是别的,你能举一些例子说明你做了什么吗? – TheBlueSky 2013-03-10 16:45:23

回答

1

UPDATEDataContextChanged WinRT for Windows 8.1支持事件。只有在针对Windows 8的WinRT或任何不支持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); 
     } 
    } 
} 

希望有帮助。

+0

我更喜欢这种方法:http://amrreda.wordpress.com/2013/01/24/winrt-datacontext-changed-event-and-trigger/ – HappyNomad 2013-06-15 05:30:57