2014-12-03 84 views
0

當我繼續嘗試創建一個簡單的示例時,我遇到了使用MVVM概念處理DataTemplates時遇到的很多問題。DataTemplates和MVVM框架

爲了熟悉DataTemplates,這個想法如下。只需創建一個DataTemplate,其中包含與我的視圖模型綁定的內容(在本例中爲顯示名稱的標籤以及顯示年齡的按鈕)。

之前的問題,這裏是代碼。

App.xaml.cs

namespace tutorial 
{ 
    /// <summary> 
    /// Interaction logic for App.xaml 
    /// </summary> 
    public partial class App : Application 
    { 
     private MainWindow mw; 
     private ViewModel vm; 

     private void Application_Startup(object sender, StartupEventArgs e) 
     { 
      vm = new ViewModel(); 
      mw = new MainWindow(); 
      mw.DataContext = ? 
      mw.Activate(); 
      mw.Show(); 
     } 
    } 
} 

ViewModel.cs

namespace tutorial 
{ 
    class ViewModel 
    { 
     private String name; 

     private int age; 

     public ViewModel() 
     { 
      Debug.WriteLine("Hello World!"); 
      name = "Hello World"; 
     } 

     public int Age 
     { 
      get {return age;} 
      set {age = value;} 
     } 

     public String Name 
     { 
      get {return name;} 
      set {name = value;} 
     } 
    } 
} 

Button.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:tutorial"> 
    <DataTemplate DataType="{x:Type local:ViewModel}"> 
     <StackPanel> 
      <Label Content="{Binding ??}" /> 
      <Button Content="{Binding ???}" /> 
     </StackPanel> 
    </DataTemplate> 
</ResourceDictionary> 

MainWindow.xaml

<Window x:Class="tutorial.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <ContentControl Content="{Binding}" /> 
</Window> 

所以,我有以下問題:

下面的代碼行<DataTemplate DataType="{x:Type local:ViewModel}">顯示該視圖模型沒有名字命名空間中的存在,顯然它吧?或者我錯過了什麼?該解決方案資源管理器中的圖像,如下圖所示:

Directory tree in solution explorer

第二個問題更重要,這也表明我現在知道如何將視圖視圖模型綁定。 中的問題App.xaml.cs,Button.xaml和可能在MainWindow.xaml中的一些錯誤,因爲我不知道MainWindow如何知道我希望它顯示哪些內容。

謝謝你的幫助。

這裏是一個link到整個項目,以先前的評論的迴應:

Problem of namespaces

+0

設置您的視圖模型爲public,它應該工作 – 2014-12-03 11:03:52

+0

對不起,它並沒有解決的第一個問題。它似乎仍然無法找到ViewModel。 – Snowflake 2014-12-03 11:08:53

+0

資源字典資源位於何處, 我想它的app.xaml..Is它?? – 2014-12-03 11:23:12

回答

1

您需要設置視圖模型是一個公共類,它在命名空間確實存在,但沒有其他類可以看到它。

namespace tutorial 
{ 
    public class ViewModel 
    { 
    } 
} 

綁定所需的數據模板

<DataTemplate DataType="{x:Type local:ViewModel}"> 
    <StackPanel> 
     <Label Content="{Binding Age}" /> 
     <Button Content="{Binding Name}" /> 
    </StackPanel> 
</DataTemplate> 

設置的DataContext

private void Application_Startup(object sender, StartupEventArgs e) 
     { 
      vm = new ViewModel(); 
      mw = new MainWindow(); 
      mw.DataContext = vm; 
      mw.Show(); 
     } 

添加資源字典

<Application.Resources> 
<ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="Button.xaml"/> 
    </ResourceDictionary.MergedDictionaries> 
</ResourceDictionary> 
</Application.Resources> 

Fixed full project available here

+0

對不起,但公開增加它似乎並沒有解決這個問題。 – Snowflake 2014-12-03 11:02:23

+0

你是什麼意思?它解決了例外嗎?我將添加一些代碼來向您顯示其他答案 – ndonohoe 2014-12-03 11:07:57

+0

它沒有解決第一個問題(請參閱新附加的圖像)。 – Snowflake 2014-12-03 11:14:30

0

你也可以這樣做。

在App.xaml.cs類

public partial class App : Application 
{ 
    protected override void OnStartup(StartupEventArgs e) 
    { 
     var vm = new ViewModel(); 
     var window = new MainWindow(); 
     window.DataContext = vm; 
     window.Show(); 
    } 
} 

在App.xaml中請務必從App.xaml中刪除的StartupUri命名空間或兩個主窗口將出現。

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Button.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

休息了ViewModel.cs代碼和Mainwindow.xaml保持不變...