从子窗口添加数据后刷新 Silverlight 数据网格

本文关键字:数据 Silverlight 数据网 网格 刷新 窗口 添加 | 更新日期: 2023-09-27 18:31:37

我想在从子窗口添加数据后刷新我的数据网格。下面是我的家.cs

 public partial class Home : Page
    {
        ServiceReference1.Service1Client webService;
        public Home()
        {
            InitializeComponent();
            webService = new ServiceReference1.Service1Client();
            webService.ReadPismaCompleted += WebService_ReadPismaCompleted;
            webService.ReadPismaAsync(0);
        }

        private void WebService_ReadPismaCompleted(object sender, ServiceReference1.ReadPismaCompletedEventArgs e)
        {
            if(e.Result != null)
            {
                dataGridPisma.ItemsSource = e.Result;
            }
        }
 private void button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ChildWindow1 childWindow = new ChildWindow1();
            childWindow.Closed += ChildWindow_Closed;
            childWindow.Show();
        }
        private void ChildWindow_Closed(object sender, System.EventArgs e)
        {
                if (( (ChildWindow1)sender).DialogResult.Value) webService.ReadPismaAsync(0);               
        }

添加数据后我没有看到任何更改(单击子窗口上的"确定"按钮不刷新数据网格)。我知道数据已添加,因为我在 SQL 服务器表中看到它,并且当我在 Web 浏览器上刷新(按 F5)时,我看到了新数据。

从子窗口添加数据后刷新 Silverlight 数据网格

WebService_ReadPismaCompleted方法中使用PagedCollectionView

private PagedCollectionView _dataGridContext;
private void WebService_ReadPismaCompleted(object sender,serviceReference1.ReadPismaCompletedEventArgs e)
    {
        if(e.Result != null)
        {
            DataGridContext = new PagedCollectionView(e.Result)
        }
    }
  public PagedCollectionView DataGridContext
  {
     get { return _dataGridContext; }
     set { 
           _dataGridContext = value; 
           OnPropertyChanged("DataGridContext");
         }
  }

并设置您的DataGrid.DataContext=DataGridContext

添加这个

   `[NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this,
                    new PropertyChangedEventArgs(propertyName));
        }
    }`

添加从二传器中删除datagridPisma.DataContext=DataGridContext