Binded Datagrid不会更新数据集更改

时间:2014-07-15 20:12:57

标签: c# wpf datagrid dataset xceed

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

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

            }
        }

}

但是,如果我更改组合框,数据集会更新,但数据网格会在第一次填充数据集时始终显示原始项目。

1 个答案:

答案 0 :(得分:0)

采取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"/>