2013-01-07 76 views
0

我正在研究一個「現代UI」應用程序,所以語法對我來說有點新,我似乎無法讓我的綁定正常工作。MVVM的DataTemplate:它到哪裏去了?

我的願望是有一個ViewModel第一設計讓我的應用程序的網頁我可以做這樣的事情有ListView並添加UserViewModel作爲一個孩子,並有DataTemplate中自動發現來創建用戶視圖,並綁定到提供的UserViewModel。

我針對爲Win 7桌面編寫的另一個應用程序做了類似的工作,它只是起作用,但對於我的生活我無法弄清楚爲什麼它不起作用。我只是在我的ListView「UserViewModel」作爲文本(沒有UserControl創建)。

這裏唯一的區別是它是我第一次使用異步函數,因爲它在Win 8開發中非常強迫你,而且這是我從WCF服務中獲得的方法數據來自。

這裏是我的視圖模型的例子:

public class UserViewModel 
{ 
    private UserDTO _user { get; set; } 

    public UserViewModel(UserDTO user) 
    { 
     _user = user; 
    } 

    public UserViewModel(int userId) 
    { 
     SetUser(userId); 
    } 

    private async void SetUser(int userId) 
    { 
     ServiceClient proxy = new ServiceClient(); 
     UserDTO referencedUser = await proxy.GetUserAsync(userId); 
    } 

    public string FirstName 
    { 
     get 
     { 
      return _user.FirstName; 
     } 
    } 

    public string LastName 
    { 
     get 
     { 
      return _user.LastName; 
     } 
    } 

    public string Email 
    { 
     get 
     { 
      return _user.email; 
     } 
    } 
} 

的觀點應該是所有XAML和應用程序資源粘在一起如下:

<UserControl x:Class="TaskClient.Views.UserView" ... 
    xmlns:root="using:TaskClient" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="30" 
    d:DesignWidth="200"> 
    <StackPanel Orientation="Horizontal" Margin="5, 0, 0 ,0" DataContext="{Binding}"> 
     <TextBlock x:Name="FirstNameLabel" Text="{Binding FirstName}"/> 
     <TextBlock x:Name="LastNameLabel" Text="{Binding LastName}"/> 
     <TextBlock x:Name="EmailLabel" Text="{Binding Email}"/> 
    </StackPanel> 
</UserControl> 

和:

<Application x:Class="TaskClient.App" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:TaskClient" 
xmlns:localData="using:TaskClient.Data" 
xmlns:vm="using:ViewModels" 
xmlns:vw="using:TaskClient.Views"> 

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 

      <!-- 
       Styles that define common aspects of the platform look and feel 
       Required by Visual Studio project and item templates 
      --> 
      <ResourceDictionary Source="Common/StandardStyles.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 

     <!-- Application-specific resources --> 

     <x:String x:Key="AppName">TaskClient</x:String> 

     <DataTemplate x:Key="vm:UserViewModel"> 
      <vw:UserView /> 
     </DataTemplate> 
    </ResourceDictionary> 
</Application.Resources> 

我試過通過各種例子搜索一個小時左右(例如。 http://joshsmithonwpf.wordpress.com/a-guided-tour-of-wpf/),並沒有找到一個適用於我的案例。

任何想法我做錯了什麼?

回答

0

可能是一個錯字,但是當您通過WCF服務獲取用戶時,您似乎沒有更新「_user」字段。您可能需要更改此:

private async void SetUser(int userId) 
{ 
    ServiceClient proxy = new ServiceClient(); 
    UserDTO referencedUser = await proxy.GetUserAsync(userId); 
} 

要這樣:

private async void SetUser(int userId) 
{ 
    ServiceClient proxy = new ServiceClient(); 
    _user = await proxy.GetUserAsync(userId); 
} 

而且我沒有看到你的ViewModel類實現INotifyPropertyChange接口,它的關鍵是WPF數據綁定。一旦這樣做了,你已經加載的用戶,則需要通知WPF有關屬性更新:

private async void SetUser(int userId) 
{ 
    ServiceClient proxy = new ServiceClient(); 
    _user = await proxy.GetUserAsync(userId); 
    NotifyOfPropertyChange(); 
} 

private void NotifyOfPropertyChange() 
{ 
    NotifyChanged("FirstName"); //This would raise PropertyChanged event. 
    NotifyChanged("LastName"); 
    NotifyChanged("Email"); 
} 
0

我只是在我的ListView「UserViewModel」獲取文本(無用戶控件創建)

DataTemplate需求與DataType財產,沒有財產x:Key來定義,對於DataTemplate得到隱式應用

<DataTemplate DataType="{x:Type vm:UserViewModel}"> 
    <vw:UserView /> 
</DataTemplate> 

指定了DataType的DataTemplate,但沒有指定x:Key是一個Implicit DataTemplate,意味着它將在WPF需要繪製指定數據類型的對象時隱式使用。

一個DataTemplate與x:Key屬性需要實際上是在你的代碼通過鑰匙來指定,如

<ListView ItemTemplate="{StaticResource MyKey}" ... /> 

而且,我不知道,如果你的問題的標題(「的DataContext的MVVM: 「)由於您的問題主體似乎沒有詢問DataContext,但是我有一個beginners article on my blog explaining the DataContext,如果您正在努力瞭解DataContext

+0

Modern UI ap沒有x:Type屬性這是我的問題。不知道它應該是什麼方式。 – Mike

+1

@Mike我沒有意識到這一點。你可以使用DataTemplateSelectors嗎?這是我在Silverlight中添加對隱式數據模板的支持之前使用的。也許像[this](http://www.wiredprairie.us/blog/index.php/archives/1705)會起作用? – Rachel

+0

嗨,是的,這可能會工作。無論如何,我必須在數據綁定之前解決一些異步加載問題。請參閱http://stackoverflow.com/a/14189904/1462330上的相關問題的答案。基本上,我現在在我的viewmodels上有一個「初始化」任務屬性。當您創建其中的一個時,您將等待初始化的任務,以便所有WCF資料都被加載並分配屬性。我在視圖的初始化任務中做了類似的事情,除了在他們結束時,我將它們綁定到他們剛剛初始化的東西似乎運作良好。 – Mike