在使用.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>
这是程序运行的截图。