绑定的数据网格在数据集更改时不更新

本文关键字:更新 数据集 数据 数据网 网格 绑定 | 更新日期: 2023-09-27 18:13:16

我创建了一个侧边面板来搜索几个数据库,这些数据库是从主菜单的组合框中选择的。

<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
            {
            }
        }
}

但是,如果我更改组合框,数据集得到更新,但是数据网格继续显示从第一次填充数据集开始的原始项目。

绑定的数据网格在数据集更改时不更新

Take UpdateSourceTrigger='PropertyChanged'

<xcdg:DataGridCollectionViewSource x:Key="Record_Search_DGCVS"
                                            Source="{Binding ElementName=Search_Mode, Path=dataSet,UpdateSourceTrigger="PropertyChanged"}"
                                            AutoCreateItemProperties="True"
                                            AutoCreateForeignKeyDescriptions="True"
                                            DefaultCalculateDistinctValues="False"/>

并将staticResource更改为DynamicResource

<xcdg:DataGridControl x:Name="Search_Record" 
                                      ItemsSource="{Binding Source={DynamicResource Record_Search_DGCVS} }"
                                      ReadOnly="True"/>