I set up LINQ-to-SQL / NorthWind in WPF.
我在WPF中设置了linqto - sql / wind。
The ListBox shows data but the DataGrid doesn't (no errors, just doesn't display anything).
ListBox显示数据,但DataGrid没有(没有错误,只是没有显示任何内容)。
I referenced WPFToolkit.dll.
我WPFToolkit.dll引用。
Why is the DataGrid not displaying the data that ListBox can?
为什么DataGrid不显示ListBox可以显示的数据?
XAML:
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;
}
}
}
2 个解决方案
#1
1
It was just a XAML issue, this fixes it:
这只是XAML的一个问题,它解决了这个问题:
<ScrollViewer>
<StackPanel>
<dg:DataGrid x:Name="TheDataGrid"/>
<ListView x:Name="TheListView" ItemTemplate="{StaticResource ShowCustomer}"/>
</StackPanel>
</ScrollViewer>
#2
0
maybe because you did not specifiy datagrid columns. try setting the datagrids AutoGenerateColumns property to true.
可能是因为您没有指定datagrid列。尝试将datagrids AutoGenerateColumns属性设置为true。
#1
1
It was just a XAML issue, this fixes it:
这只是XAML的一个问题,它解决了这个问题:
<ScrollViewer>
<StackPanel>
<dg:DataGrid x:Name="TheDataGrid"/>
<ListView x:Name="TheListView" ItemTemplate="{StaticResource ShowCustomer}"/>
</StackPanel>
</ScrollViewer>
#2
0
maybe because you did not specifiy datagrid columns. try setting the datagrids AutoGenerateColumns property to true.
可能是因为您没有指定datagrid列。尝试将datagrids AutoGenerateColumns属性设置为true。