Привязка сущностей к сетке данных в wpf - PullRequest
2 голосов
/ 22 октября 2011

В winform я делаю это:

    using (vbdadvertisementEntities context = new vbdadvertisementEntities())
    {
        var q = context.customers;
        customerBindingSource.DataSource = q;
        dataGridView.DataSource = customerBindingSource;
    }

Тогда я попробовал это в wpf, но сетка данных просто пуста:

    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>

1 Ответ

4 голосов
/ 22 октября 2011

Вы должны указать ItemsSource, указав, что DataContext просто предоставит контекст для привязок данных.

Как,

<DataGrid AutoGenerateColumns="False" Grid.Row="1" Name="MainDataGrid" ItemsSource="{Binding}" />
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...