我创建了一个侧面板来搜索多个数据库,这些数据库是从主菜单的组合框中选择的。
<UserControl x:Class="Sum.SideRecordSearch"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Name="Search_Mode">
<UserControl.Resources>
<ResourceDictionary>
<xcdg:DataGridCollectionViewSource x:Key="Record_Search_DGCVS"
Source="{Binding ElementName=Search_Mode, Path=dataSet}"
AutoCreateItemProperties="True"
AutoCreateForeignKeyDescriptions="True"
DefaultCalculateDistinctValues="False"/>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<xcdg:DataGridControl x:Name="Search_Record"
ItemsSource="{Binding Source={StaticResource Record_Search_DGCVS} }"
ReadOnly="True"/>
</Grid>
我第一次使用以下代码运行应用程序时填充了数据:
public partial class SideRecordSearch : UserControl
{
public DataTable dataSet
{
get;
set;
}
public SideRecordSearch(MainWindow Window)
{
this.dataSet = getData(Window.System_Selected);
InitializeComponent();
}
internal void Change_System_Subsystem(string System_Selected)
{
this.dataSet = getData(System_Selected);
Search_Record.Items.Refresh();
}
}
在 MainWindow 类中,每次更新组合框时我都会放置一个 void 方法:
public partial class MainWindow : RibbonWindow
{
internal string System_Selected;
SideRecordSearch Search_Panel;
public MainWindow()
{
InitializeComponent();
System_Selected = ((ComboBoxItem)IP_System.SelectedItem).Name;
Search_Panel = new SideRecordSearch(this);
this.QuickPanel.Children.Add(Search_Panel);
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
System_Selected = ((ComboBoxItem)System.SelectedItem).Name;
Search_Panel.Change_System_Subsystem(System_Selected);
}
catch
{
}
}
}
但是,如果我更改组合框,数据集会更新,但数据网格会继续显示第一次填充数据集时的原始项目。