2015-10-12 40 views
0

我在這裏有一個簡單的應用程序,它使用可觀察的集合併爲每個項目生成tabcontrol選項卡。但是,當我嘗試添加其他選項卡時失敗。我還注意到,當我試圖在集合中打印數據時,看起來我無法做到這一點。這兩行代碼都會出現以下錯誤。WPF將項目添加到可觀察的集合和打印。返回對象引用錯誤

錯誤1的對象引用是所必需的非靜態字段, 方法或屬性

InitializeComponent(); 
//ERRORS on both lines below 
Console.WriteLine(ViewModel.TabItems); 
ViewModel.AddContentItem(); 

MainWindow.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Windows; 

namespace WpfApplication1 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      //ERRORS on both lines below 
      Console.WriteLine(ViewModel.TabItems); 
      ViewModel.AddContentItem(); 
     } 
    } 
} 

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:data="clr-namespace:WpfApplication1" 
     Title="MainWindow" Height="350" Width="250"> 

    <Window.DataContext> 
     <data:ViewModel/> 
    </Window.DataContext> 

    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

     <TabControl ItemsSource="{Binding TabItems}" Grid.Row="1" Background="LightBlue"> 
      <TabControl.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal" VerticalAlignment="Center"> 
         <TextBlock Text="{Binding Header}" VerticalAlignment="Center"/> 
        </StackPanel> 
       </DataTemplate> 
      </TabControl.ItemTemplate> 

      <TabControl.ContentTemplate> 
       <DataTemplate> 
        <TextBlock 
        Text="{Binding Content}" /> 
       </DataTemplate> 
      </TabControl.ContentTemplate> 
     </TabControl> 

    </Grid> 
</Window> 

ViewModel.cs

using System; 
using System.Collections.ObjectModel; 
using System.Windows; 

namespace WpfApplication1 
{ 
    public class ViewModel : ObservableObject 
    { 
     private ObservableCollection<TabItem> tabItems; 

     public ObservableCollection<TabItem> TabItems 
     { 
      get { return tabItems ?? (tabItems = new ObservableCollection<TabItem>()); } 
     } 

     public ViewModel() 
     { 
      TabItems.Add(new TabItem { Header = "One", Content = DateTime.Now.ToLongDateString() }); 
      TabItems.Add(new TabItem { Header = "Two", Content = DateTime.Now.ToLongDateString() }); 
      TabItems.Add(new TabItem { Header = "Three", Content = DateTime.Now.ToLongDateString() }); 
     } 

     public void AddContentItem() 
     { 
      TabItems.Add(new TabItem { Header = "Three", Content = DateTime.Now.ToLongDateString() }); 
     } 
    } 



    public class TabItem 
    { 
     public string Header { get; set; } 
     public string Content { get; set; } 
    } 
} 

ObservableObject.cs

using System; 
using System.ComponentModel; 
using System.Runtime.CompilerServices; 

namespace WpfApplication1 
{ 
    public class ObservableObject : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

     public void NotifyPropertyChanged([CallerMemberName] string propertyName = null) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 
} 

回答

2

你應該這樣做,而不是

namespace WpfApplication1 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      var viewModel = (ViewModel) this.DataContext; 

      Debug.WriteLine(viewModel.TabItems); 
      viewModel.AddContentItem(); 
     } 
    } 
} 

已更新感謝King King

+0

'DataContext'只是一個對象,所以您需要明確地將其轉換爲'ViewModel'。 –

相關問題