2011-10-22 63 views
2

的DataGrid在winform,我這樣做:綁定實體在WPF

​​

然後,我想這在WPF但DataGrid中只顯示空白:

CollectionViewSource customerViewSource = (CollectionViewSource) FindResource("customerViewSource"); 
    using (vbdadvertisementEntities context = new vbdadvertisementEntities()) 
    { 
     var q = context.customers; 
     customerViewSource.Source = q; 
    } 

XAML:

<Window x:Class="VBDAdvertisement.WCustomerMain" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="WCustomerMain" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="379" Width="654" xmlns:my="clr-namespace:VBDAdvertisement" Loaded="Window_Loaded"> 
    <Window.Resources> 
     <CollectionViewSource x:Key="customerViewSource"/> 
    </Window.Resources> 
    <Grid DataContext="{StaticResource customerViewSource}"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="56*" /> 
      <RowDefinition Height="284*" /> 
     </Grid.RowDefinitions> 
     <DataGrid AutoGenerateColumns="False" Grid.Row="1" Name="MainDataGrid" DataContext="{Binding}" /> 
    </Grid> 
</Window> 

回答

4

你必須指定ItemsSource,指定DataContext只是提供一個cont分機用於數據綁定。

一樣,

<DataGrid AutoGenerateColumns="False" Grid.Row="1" Name="MainDataGrid" ItemsSource="{Binding}" /> 
+0

,也如果你對你的DataGrid(在XAML中或通過代碼隱藏)設置的AutoGenerateColumns爲true – whoisthis

+0

沒有指定的列明白了,非常感謝! – JatSing