WPF DataGrid与EF数据库的第一个数据绑定问题

时间:2016-11-08 13:29:45

标签: c# wpf visual-studio data-binding wpfdatagrid

我正在尝试绑定mssql db中的表中的数据,我已按照MSDN建议的步骤进行操作。问题是我的数据网格只显示标题,然后重复我的数据库所拥有的行数的标题。 Screenshot

这是我的代码

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        System.Windows.Data.CollectionViewSource testbazaEntitiesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("testbazaEntitiesViewSource")));
        // Load data by setting the CollectionViewSource.Source property:
        // testbazaEntitiesViewSource.Source = [generic data source]
        System.Windows.Data.CollectionViewSource tabelaViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("tabelaViewSource")));
        // Load data by setting the CollectionViewSource.Source property:
        // tabelaViewSource.Source = [generic data source]
    }
}
}

这是XAML

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Window.Resources>
    <CollectionViewSource x:Key="testbazaEntitiesViewSource" d:DesignSource="{d:DesignInstance {x:Type local:testbazaEntities}, CreateList=True}"/>
    <CollectionViewSource x:Key="testbazaEntitiestabelaViewSource" Source="{Binding tabela, Source={StaticResource testbazaEntitiesViewSource}}"/>
    <CollectionViewSource x:Key="tabelaViewSource" d:DesignSource="{d:DesignInstance {x:Type local:tabela}, CreateList=True}"/>
</Window.Resources>
<Grid DataContext="{StaticResource testbazaEntitiestabelaViewSource}">
    <DataGrid x:Name="tabelaDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding Source={StaticResource tabelaViewSource}}" RowDetailsVisibilityMode="VisibleWhenSelected">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="dugovanjaColumn" Binding="{Binding dugovanja}" Header="dugovanja" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="iD_brojColumn" Binding="{Binding ID_broj}" Header="ID broj" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="imeColumn" Binding="{Binding ime}" Header="ime" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="oblik_nosaColumn" Binding="{Binding oblik_nosa}" Header="oblik nosa" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="plataColumn" Binding="{Binding plata}" Header="plata" Width="SizeToHeader"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>
</Window>

数据库有2行数据,我没有设法显示任何数据,对于wpf,c#和编码一般都很新,感谢阅读

0 个答案:

没有答案