tags:

views:

418

answers:

2

I set up LINQ-to-SQL / NorthWind in WPF.

The ListBox shows data but the DataGrid doesn't (no errors, just doesn't display anything).

I referenced WPFToolkit.dll.

Why is the DataGrid not displaying the data that ListBox can?

XAML:

<Window x:Class="TestLinq343.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <DataTemplate x:Key="ShowCustomer">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding CategoryID}"/>
                <TextBlock Text=": "/>
                <TextBlock Text="{Binding ProductName}"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <dg:DataGrid x:Name="TheDataGrid" AutoGenerateColumns="True"></dg:DataGrid>
        <ListBox x:Name="TheListBox" ItemTemplate="{StaticResource ShowCustomer}"/>
    </Grid>
</Window>

code behind:

using System.Linq;
using System.Windows;
using TestLinq343.Model;
using Microsoft.Windows.Controls;

namespace TestLinq343
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            NorthwindDataContext db = new NorthwindDataContext();
            var sortedProducts =
                from p in db.Products
                orderby p.UnitsInStock descending
                select p;

            TheDataGrid.ItemsSource = sortedProducts;
            TheListBox.ItemsSource = sortedProducts;
        }
    }
}
A: 

maybe because you did not specifiy datagrid columns. try setting the datagrids AutoGenerateColumns property to true.

Joachim Kerschbaumer
tried that, still blank, autogeneratecolumns is true be default, in the walkthrough I'm following http://windowsclient.net/wpf/wpf35/wpf-35sp1-toolkit-datagrid-feature-walkthrough.aspx, it defines the ItemsSource in the XAML but that shouldn't matter, should it?
Edward Tanguay
A: 

It was just a XAML issue, this fixes it:

<ScrollViewer>
    <StackPanel>
        <dg:DataGrid x:Name="TheDataGrid"/>
        <ListView x:Name="TheListView" ItemTemplate="{StaticResource ShowCustomer}"/>
    </StackPanel>
</ScrollViewer>
Edward Tanguay