2013-04-08 71 views
0

我正在跟隨一些例子,我無法在我的數據網格中顯示數據。值得一提的是,我在從db中獲取數據時,看着debuger。WPF不顯示數據網格內的數據

MainWindow.xaml.cs

public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

    } 

public partial class MainWindow : Window 
{ 
    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     using (session...) 
     { 
      using (transaction...) 
      { 
       var properties = new List<MyProperty>(); 
       // this variable is populated inside debugger 
       properties = session.Query<MyProperty>().ToList(); 
       if (properties != null) 
       { 
        MRDataGrid.Columns[0].Visibility = System.Windows.Visibility.Hidden; 
        MRDataGrid.Columns[1].Visibility = System.Windows.Visibility.Hidden; 
        MRDataGrid.Columns[8].Visibility = System.Windows.Visibility.Hidden; 
       } 
      } 
     } 
    } 
} 

MainWindow.xaml

<Window x:Class="MyProject.WPF.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" 
     WindowStartupLocation="CenterScreen" BorderBrush="Black" 
     Background="AntiqueWhite" ResizeMode="NoResize" 
     Loaded="Window_Loaded"> 
    <Grid> 
     <DataGrid AutoGenerateColumns="False" Height="202" HorizontalAlignment="Left" Margin="22,82,0,0" 
        Name="MRDataGrid" VerticalAlignment="Top" Width="461" ItemsSource="{Binding}"/> 
    </Grid> 
</Window> 

更新

  • 我已經改變到的AutoGenerateColumns = 「真」 在MainWindow.xaml
  • 新增了MRDataGrid.ItemsSource =屬性;在xaml.cs代碼
  • 我應該在MainWindow.xaml中的ItemSource中放置什麼?
+1

你沒有設置ItemsSource – Paparazzi 2013-04-08 18:44:25

+0

嗯......嘗試'AutoGenerateColumns =「True」'? – McGarnagle 2013-04-08 18:44:27

+0

你將DataContext分配給MRDataGrid聽起來像是一個綁定/路徑問題你有 – MethodMan 2013-04-08 18:44:38

回答

1

您沒有設置ItemsSource="{Binding}"的任何上下文來處理。

這樣做myDataGrid.DataContext=...或綁定的數據上下文或改變{Binding}點在你需要它

如:

if (properties != null) 
{ 
    myDataGrid.ItemsSource = properties; 
    ... 
+0

我更新了問題,不確定在ItemsSource =裏面放什麼? – user1765862 2013-04-08 18:53:38

+0

@ user1765862嘗試編輯,您也可以嘗試將屬性分配給DataContext – 2013-04-08 18:57:52

0

性能= session.Query()ToList();

MRDataGrid.ItemsSource = properties;

0

您的數據網格沒有列。你或者需要自己聲明或設置AutoGenerateColumns="True"你還需要指定項目源,如Sten Petrov所說的