从childwindow添加数据后,silverlight datagrid刷新

时间:2016-01-22 14:21:10

标签: c# wpf silverlight datagrid

我想在从childwindow添加数据后刷新我的数据网格。 以下是My Home.xaml.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)时,我看到了新数据。

2 个答案:

答案 0 :(得分:0)

PagedCollectionView方法

中使用WebService_ReadPismaCompleted
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

答案 1 :(得分:0)

添加此

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

从您的二传手中添加删除datagridPisma.DataContext=DataGridContext