2015-12-14 95 views
1

我試圖做一個簡單的WPF項目,我有一個問題: DataGrid綁定到一個ObservableCollection,它不顯示 的數據,但它顯示了正確的它的外觀WPF DataGrid中不顯示數據,但秀行

WPF DataGrid showing correct number of rows, but not showing data

這裏rows.Here的量是XAML:

<Window x:Class="008.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:008" 
     mc:Ignorable="d" 
     Title="MainWindow"> 

    <StackPanel> 
     <StackPanel Orientation="Horizontal" Margin="10"> 
      <Button Name="AddButton" HorizontalAlignment="Left" Margin="10">Add an element</Button> 
      <Button Name="DeleteOldButton" HorizontalAlignment="Left" Margin="10">Delete old files</Button> 
      <Button Name="ShowPop" HorizontalAlignment="Left" Margin="10">Show most popular element</Button> 
     </StackPanel> 

     <DataGrid Name="dgrid" CanUserResizeColumns="True" 
        CanUserAddRows="False" 
        IsReadOnly="True" 
        DataContext="files" 
        ItemsSource="{Binding}" 

        Width="Auto"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Header="Name" Binding="{Binding Name}"/> 
       <DataGridTextColumn Header="Date created" Binding="{Binding Created}"/> 
       <DataGridTextColumn Header="Times Open" Binding="{Binding TimesOpen}"/> 

      </DataGrid.Columns> 
     </DataGrid> 

    </StackPanel> 
</Window> 

而這裏的代碼:

public partial class MainWindow : Window 
    { 
    ObservableCollection<File> files; 

    public MainWindow() 
    { 

     InitializeComponent(); 
     files = new ObservableCollection<File>(); 
     files.Add(new File("r", DateTime.Now)); 
     files.Add(new File("o", DateTime.Now)); 
     files.Add(new File("a", DateTime.Now)); 
     files.Add(new File("d", DateTime.Now)); 

    } 

} 

我已經嘗試了窗口的DataContext設置爲文件,也沒有工作

UPD:這裏的文件類:

class File 
    { 
     public string Name { get; set; } 
     public DateTime Created { get; set; } 
     public int TimesOpen { get; set; } 

     public File(string s, DateTime d) 
     { 
      Created = d; 
      Name = s; 
      TimesOpen = 0; 
     } 
     public void Open() 
     { 
      TimesOpen++; 
     } 
    } 

UPD:實現了INotifyPropertyChanged的。沒有幫助。該文件定義如下:

class File: INotifyPropertyChanged 

    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     private string name; 
     public string Name 
     { 
      get { return name; } 
      set 
      { 
       if (!value.Equals(name,StringComparison.InvariantCulture)) { 
        OnPropertyChanged("Name"); 
        name = value; 
       }      
      } 

     } 
     private DateTime created; 
     public DateTime Created 
     { 
      get { return created; } 
      set 
      { 
       if (!created.Equals(value)) 
       { 
        OnPropertyChanged("Created"); 
        created = value; 
       } 
      } 
     } 
     private int times_open; 
     public int TimesOpen 
     { 
      get { return times_open; } 
      set 
      { 
       if (times_open != value) 
       { 
        OnPropertyChanged("TimesOpen"); 
        times_open = value; 
       } 
      } 
     } 

     public File(string s, DateTime d) 
     { 
      Created = d; 
      Name = s; 
      TimesOpen = 0; 
     } 

     void OnPropertyChanged(string name) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(name)); 
      } 
     } 

     public void Open() 
     { 
      TimesOpen++; 
     } 
    } 
} 
+0

你能告訴我們文件類型的定義嗎? –

+0

是的,當然,請參閱UPD –

+0

您是否嘗試過使文件實現INotifyPropertyChanged?把它放在Name,Created和TimeOpen屬性上。 –

回答

2

有你做錯了 一些事情 - 你應該設置的datacontext爲窗口
- 你的項目源爲界的文件。但等待..
- 文件需要在你的DataContext屬性,即主窗口
- 另外,你應該想想你的模型

<DataGrid Name="dgrid" CanUserResizeColumns="True" 
     CanUserAddRows="False" 
     IsReadOnly="True" 
     ItemsSource="{Binding Files}" 
     Width="Auto"> 

    <DataGrid.Columns> 
     <DataGridTextColumn Header="Name" Binding="{Binding Name}"/> 
     <DataGridTextColumn Header="Date created" Binding="{Binding Created}"/> 
     <DataGridTextColumn Header="Times Open" Binding="{Binding TimesOpen}"/> 
    </DataGrid.Columns> 
</DataGrid> 

在你的代碼執行INotifyPropertyChanged的使用背後

public partial class MainWindow : Window 
{ 
    public ObservableCollection<File> Files {get; set;} 


    public MainWindow() 
    { 

     InitializeComponent(); 
     DataContext = this; 
     Files = new ObservableCollection<File>(); 
     Files.Add(new File("r", DateTime.Now)); 
     Files.Add(new File("o", DateTime.Now)); 
     Files.Add(new File("a", DateTime.Now)); 
     Files.Add(new File("d", DateTime.Now)); 
     Files.Add(new File("d", DateTime.Now)); 
     Files.Add(new File("d", DateTime.Now)); 
    } 
} 
+0

實施INotifyPropertyChanged,嘗試設置DataContext這兩個文件,使文件公共屬性 - 什麼都沒有 –

+0

@ small-j不,你不應該設置datacontext文件。如上所示,在mainwindow的構造函數中將datacontext設置爲this。然後您將ItemsSource設置爲文件。你可以複製上面的代碼。你甚至不需要實現INotifyPropertyChanged來查看網格中的數據。當您想要稍後通知UI數據更改時,INotifyPropertyChanged會很有用。 – amuz

+0

謝謝大家 –

0

試試這個.. MainWindow.xaml.cs

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 

     InitializeComponent(); 
     Files = new ObservableCollection<File>(); 
     Files.Add(new File("r", DateTime.Now)); 
     Files.Add(new File("o", DateTime.Now)); 
     Files.Add(new File("a", DateTime.Now)); 
     Files.Add(new File("d", DateTime.Now)); 
     DataContext = this; 
    } 
} 

public ObservableCollection<File> Files { get; set; } 

MainWindow.xaml(snippet):

<DataGrid Name="dgrid" CanUserResizeColumns="True" 
        CanUserAddRows="False" 
        IsReadOnly="True" 
        ItemsSource="{Binding Files}" 
        Width="Auto"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="Name" Binding="{Binding Name}"/> 
     <DataGridTextColumn Header="Date created" Binding="{Binding Created}"/> 
     <DataGridTextColumn Header="Times Open" Binding="{Binding TimesOpen}"/> 
    </DataGrid.Columns> 
</DataGrid>