sunnyzls
sunnyzls
采纳率100%
2020-07-29 01:03 阅读 102

在WPF的Page中添加的数据源控件DataGrid不显示数据

20

在使用.Net Framework的WPF创建桌面应用时,从现有数据库向项目中添加了数据源。

如果将数据源对应的控件添加到窗体(Window)上,那么数据库中的数据可以正常显示。

但是如果将数据源对应的控件添加到页面(Page)上,那么数据库中的数据就显示不出来。

程序中没有添加C#代码。
下面是MainWindow.xaml的代码:

<Window x:Class="DefenseFramework.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:DefenseFramework"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
    <Window.Resources>
        <local:DataSet1 x:Key="dataSet1"/>
        <CollectionViewSource x:Key="questionViewSource" Source="{Binding Question, Source={StaticResource dataSet1}}"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource questionViewSource}">
        <Grid.RowDefinitions>
            <RowDefinition Height="249*"/>
            <RowDefinition Height="170*"/>
        </Grid.RowDefinitions>
        <Frame Source="Page1.xaml" Grid.RowSpan="1"></Frame>
        <DataGrid x:Name="questionDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" RowDetailsVisibilityMode="VisibleWhenSelected" Grid.Row="1">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="idColumn" Binding="{Binding Id}" Header="Id" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="askColumn" Binding="{Binding Ask}" Header="Ask" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="answerColumn" Binding="{Binding Answer}" Header="Answer" Width="SizeToHeader"/>
                <DataGridCheckBoxColumn x:Name="testedColumn" Binding="{Binding Tested}" Header="Tested" Width="SizeToHeader"/>
                <DataGridCheckBoxColumn x:Name="learnedColumn" Binding="{Binding Learned}" Header="Learned" Width="SizeToHeader"/>
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>

下面是Page1.xaml的代码:

<Page x:Class="DefenseFramework.Page1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:DefenseFramework"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page1">
    <Page.Resources>
        <local:DataSet1 x:Key="dataSet1"/>
        <CollectionViewSource x:Key="questionViewSource" Source="{Binding Question, Source={StaticResource dataSet1}}"/>
    </Page.Resources>

    <Grid DataContext="{StaticResource questionViewSource}">
        <DataGrid x:Name="questionDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="10" RowDetailsVisibilityMode="VisibleWhenSelected">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="idColumn" Binding="{Binding Id}" Header="Id" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="askColumn" Binding="{Binding Ask}" Header="Ask" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="answerColumn" Binding="{Binding Answer}" Header="Answer" Width="SizeToHeader"/>
                <DataGridCheckBoxColumn x:Name="testedColumn" Binding="{Binding Tested}" Header="Tested" Width="SizeToHeader"/>
                <DataGridCheckBoxColumn x:Name="learnedColumn" Binding="{Binding Learned}" Header="Learned" Width="SizeToHeader"/>
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Page>

这是程序运行的截图。
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    dabocaiqq dabocaiqq 2020-07-29 12:31
  • soar3033 soar3033 2020-07-29 11:57
    <Page x:Class="DefenseFramework.Page1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
          xmlns:local="clr-namespace:DefenseFramework"
          mc:Ignorable="d" 
          d:DesignHeight="450" d:DesignWidth="800"
          Title="Page1">
        <Page.Resources>
            <local:DataSet1 x:Key="dataSet1"/>
            <CollectionViewSource x:Key="questionViewSource" Source="{Binding Question, Source={StaticResource dataSet1}}"/>
        </Page.Resources>
    
        <Grid DataContext="{StaticResource questionViewSource}">
            <DataGrid x:Name="questionDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="10" RowDetailsVisibilityMode="VisibleWhenSelected" Grid.Row="1">
                <DataGrid.Columns>
                    <DataGridTextColumn x:Name="idColumn" Binding="{Binding Id}" Header="Id" Width="SizeToHeader"/>
                    <DataGridTextColumn x:Name="askColumn" Binding="{Binding Ask}" Header="Ask" Width="SizeToHeader"/>
                    <DataGridTextColumn x:Name="answerColumn" Binding="{Binding Answer}" Header="Answer" Width="SizeToHeader"/>
                    <DataGridCheckBoxColumn x:Name="testedColumn" Binding="{Binding Tested}" Header="Tested" Width="SizeToHeader"/>
                    <DataGridCheckBoxColumn x:Name="learnedColumn" Binding="{Binding Learned}" Header="Learned" Width="SizeToHeader"/>
                </DataGrid.Columns>
            </DataGrid>
    
        </Grid>
    </Page>
    
    点赞 评论 复制链接分享
  • sunnyzls sunnyzls 2021-01-30 19:23

    我自己找到问题的原因了。没有在Page1.cs中对xaml文件中的资源进行初始化。初始化的代码可以参见MainWindow.cs中的代码。

        <Page.Resources>
            <local:DataSet1 x:Key="dataSet1"/>
            <CollectionViewSource x:Key="questionViewSource" Source="{Binding Question, Source={StaticResource dataSet1}}"/>
        </Page.Resources>
    点赞 评论 复制链接分享

相关推荐