2013-03-08 46 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