我在这里检查了各种链接以及处理各种数据网格的网页没有显示数据,但我没有遇到与我遇到的问题类似的情况。
Not refreshing in WPF with DataGrid
WPF Does not show data inside datagrid
How to get data into datagrid using dataset?
DataGridView doesn't show data: DataSet is empty
在C#WPF中,我创建了一个应用程序,在运行时,它会显示一个主菜单屏幕,其中包含用户页面的选项卡和页面顶部的不同按钮,如:主页,退出,更新和设置。我遇到的问题是在我的设置菜单中。当用户点击MainWindow上的设置按钮时,会弹出一个SettingsMainWindow。白色部分是用户点击以进入不同设置页面的链接,右侧窗口根据点击的内容滑动到下一页。
SettingsMainWindow.xaml
``
<!-- Define supported transitions -->
<NavigationTransition:NavigationCubeTransition x:Key="CubeTransition" Duration="0:0:0.8"/>
<NavigationTransition:NavigationSlideTransition x:Key="SlideTransition" Duration="0:0:0.8"/>
<NavigationTransition:NavigationFlipTransition x:Key="FlipTransition" Duration="0:0:0.8"/>
<BooleanToVisibilityConverter x:Key="bvc"/>
</Window.Resources>
<DockPanel Grid.RowSpan="5" Height="auto" HorizontalAlignment="Stretch" Name="dockPanelLogin" VerticalAlignment="Stretch" Width="auto" Opacity="10" Grid.ColumnSpan="2" OpacityMask="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}" >
<DockPanel.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</DockPanel.Background>
<Grid Margin="100">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<DockPanel Grid.Column="0" LastChildFill="True" Margin="5">
<!-- Each ComboBoxItem contains the transition in its Tag property
so that the TransitionContainer can directly databind to the
selected transition -->
<ComboBox x:Name="transitionComboBox" SelectedIndex="0" DockPanel.Dock="Bottom" Margin="0,3,0,0">
<!--<ComboBoxItem Tag="{StaticResource CubeTransition}">Cube</ComboBoxItem>-->
<ComboBoxItem Tag="{StaticResource SlideTransition}">Slide</ComboBoxItem>
<!--<ComboBoxItem Tag="{StaticResource FlipTransition}">Flip</ComboBoxItem>-->
</ComboBox>
<ListBox Grid.Column="0"
ItemsSource="{Binding Views}" IsSynchronizedWithCurrentItem="True"
SelectedIndex="0">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" Padding="10"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
<Border Grid.Column="1" Margin="5" BorderBrush="#FF7F9DB9" BorderThickness="1">
<!-- The ContentProperty is not bound directly to the SelectedItem of the ListBox because the
GoForward property must be updated BEFORE the content changes. The CurrentContent property
is defined in the ViewModel class and updated everytime the selection of the ListBox changes,
after setting up the GoForward value. -->
<NavigationTransition:NavigationPresenter
Content="{Binding CurrentContent}"
Transition="{Binding ElementName=transitionComboBox, Path=SelectedItem.Tag}"
GoForward="{Binding DataContext.GoForward, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type NavigationTransition:NavigationSlideTransition}}}"/>
</Border>
<!--Region Editor-->
<Grid Grid.RowSpan="100" Grid.ColumnSpan="100"
Background="#88000000" Visibility="{Binding IsShowingPage3View, Converter={StaticResource bvc}, FallbackValue=Collapsed}">
<View:Page3View Margin="0, 200, 0, 0"
x:Name="page3View" DataContext="{Binding}" Width="800" Height="700"/>
</Grid>
</Grid>
</DockPanel>
我的问题是在下面页面的数据网格中显示Access数据库中的数据。此网格应在加载时使用区域表信息自动填充,用户可以在其中添加其他区域信息。运行时,下面应该显示四列。我已经在我的应用程序的其他地方实现了相同的功能,它不在菜单中,它工作得很好。在调试时,它表明它正在正确读取表,但它只是没有显示也没有给出任何错误。我的问题是如何让它在菜单页面中显示所需的数据。
Page3View.xaml
<Border BorderBrush="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}"
BorderThickness="1"
Background="{DynamicResource {x:Static SystemColors.GradientActiveCaptionBrushKey}}"
Padding="20">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="1">
<Rectangle HorizontalAlignment="Left" Width="100" Height="6" Fill="{DynamicResource {x:Static SystemColors.MenuHighlightBrushKey}}" />
<TextBlock FontSize="24" Foreground="#444444" FontWeight="Bold" Text="REGION-DISTRICT NAME EDITOR" />
</StackPanel>
<StackPanel Grid.Row="1" Height="10"/>
<DataGrid x:Name="dg" Grid.Row="2" Height="Auto" VerticalAlignment="Stretch"
VirtualizingStackPanel.IsVirtualizing="True"
CanUserAddRows="True"
CanUserDeleteRows="True"
CanUserResizeRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="True"
CanUserSortColumns="False"
VirtualizingStackPanel.VirtualizationMode="Recycling"
AutoGenerateColumns="True">
</DataGrid>
<!--<StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right" Grid.ColumnSpan="4"
IsEnabled="{Binding TaskbarProgressValue, Converter={StaticResource ProgressBarStartButtonEnabledConverter}}">
<Button Style="{StaticResource PanelCloseButtonStyle}" Margin="0" Click="btnClose_Click" />
</StackPanel>-->
<StackPanel Margin="0, 10, 0, 0" Grid.Row="4" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Height="Auto" Click="btnSubmit_Click" Content="OK" Command="{Binding ToggleRegionEditorsCommand}" CommandParameter="{StaticResource False}" Width="90"/>
<StackPanel Width="10" />
<Button Height="Auto" Click="btnClose_Click" Content="Cancel" Command="{Binding ToggleRegionEditorsCommand}" CommandParameter="{StaticResource False}" Width="90" />
</StackPanel>
<!--Added for page 3 region editor-->
<!--<Grid Grid.RowSpan="100" Grid.ColumnSpan="100"
Background="#88000000" Visibility="{Binding IsShowingPage3View, Converter={StaticResource bvc}, FallbackValue=Collapsed}">
<View:Page3View Margin="0, 200, 0, 0"
x:Name="page3View" DataContext="{Binding}" Width="800" Height="700"/>
</Grid>-->
</Grid>
</Border>
Page3View.xaml.cs
{
/// <summary>
/// Interaction logic for Page3View.xaml Region Distric Editor
/// </summary>
public partial class Page3View : UserControl
{
public Page3View()
{
InitializeComponent();
}
private IDbDriver Database { get; set; }
private DataHelper DataHelper { get; set; }
public void Init(DataHelper dataHelper)
{
if (dg.ItemsSource != null)
{
dg.ItemsSource = null;
}
Database = dataHelper.Database;
DataHelper = dataHelper;
Query selectQuery = Database.CreateQuery("SELECT DRS, RegCode, DISTRICT, DistCode FROM [codedistrict1] ORDER BY DRS, DISTRICT");
DataTable dt = Database.Select(selectQuery);
dg.ItemsSource = (dt).DefaultView;
}
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
if (dg.ItemsSource != null)
{
DataView dv = dg.ItemsSource as DataView;
if (dv != null && dv.Table != null)
{
DataTable dt = dv.Table;
if (dt != null && Database != null)
{
try
{
string querySyntax = "DELETE * FROM [codedistrict1]";
if (Database.ToString().ToLower().Contains("sql"))
{
querySyntax = "DELETE FROM [codedistrict1]";
}
Query deleteQuery = Database.CreateQuery(querySyntax);
Database.ExecuteNonQuery(deleteQuery);
DataRow[] rows = dt.Select(String.Empty, String.Empty, DataViewRowState.CurrentRows);
foreach (DataRow row in rows)
{
Query insertQuery = Database.CreateQuery("INSERT INTO [codedistrict1] (DRS, RegCode, DISTRICT, DistCode) VALUES (" +
"@DRS, @RegCode, @DISTRICT, @DistCode)");
insertQuery.Parameters.Add(new QueryParameter("@DRS", DbType.String, row[0].ToString()));
insertQuery.Parameters.Add(new QueryParameter("@RegCode", DbType.String, row[1].ToString()));
insertQuery.Parameters.Add(new QueryParameter("@DISTRICT", DbType.String, row[2].ToString()));
insertQuery.Parameters.Add(new QueryParameter("@DistCode", DbType.String, row[3].ToString()));
Database.ExecuteNonQuery(insertQuery);
}
MessageBox.Show("Changes were committed to the database successfully.", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
MessageBox.Show("Changes were not committed to the database successfully. Error: " + ex.Message, "Failure", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
}
}
}
}
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
}
}
}
DataHelper.cs
public class DataHelper : DataHelperBase
{
#region Members
private bool _isShowingPage3View = false;
#endregion
#region Properties
// Shows the region districts update page for the settings page
public bool IsShowingPage3View
{
get
{
return this._isShowingPage3View;
}
set
{
if (!IsShowingPage3View == value)
{
this._isShowingPage3View = value;
RaisePropertyChanged("IsShowingPage3View");
}
}
}
// This is for the region district editor on the settings page
public ICommand TogglePage3ViewCommand { get { return new RelayCommand<bool> TogglePage3ViewCommandExecute); } }
private void TogglePage3ViewCommandExecute(bool show)
{
IsShowingPage3View = show;
}
private void btnSettings_Click(object sender, RoutedEventArgs e)
{
{
Regions.View.Page3View.Int(this.DataHelper);
}
}
使用上面的代码我现在收到一个错误,它不包含Int的定义,尽管它是在Page3View上定义的。它应该与这段代码绑定。
public void Init(DataHelper dataHelper)
{
if (dg.ItemsSource != null)
{
dg.ItemsSource = null;
}
Database = dataHelper.Database;
DataHelper = dataHelper;
Query selectQuery = Database.CreateQuery("SELECT DRS, RegCode, DISTRICT, DistCode FROM [codedistrict1] ORDER BY DRS, DISTRICT");
DataTable dt = Database.Select(selectQuery);
dg.ItemsSource = (dt).DefaultView;
}