【问题标题】:silverlight datagrid refresh after add data from childwindow从子窗口添加数据后,silverlight 数据网格刷新
【发布时间】:2016-04-29 04:23:33
【问题描述】:

我想在从子窗口添加数据后刷新我的数据网格。 下面是我的 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 Server 表中看到了该数据,并且当我在网络浏览器上刷新(按 F5)时,我看到了新数据。

【问题讨论】:

    标签: c# wpf silverlight datagrid


    【解决方案1】:

    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

    【讨论】:

    • link to foto OnProprertyChanged 出现错误
    • 新需要在你的类中实现INotifyPropertyChanged 接口。或者尝试不调用 OnPropertyChanged
    • 你能分享一下新的实现吗?
    • 在类中我需要实现 INOtifyPropertyChanged?在服务中,我有 public ObservableCollection ReadPisma(int barkod),我的班级是 Pismo,但我没有选择 INotifyPropertyChanged 的​​选项。
    • 你的class Home需要实现INotifyPropertyChanged & set this.DataContext = this;
    【解决方案2】:

    添加这个

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

    从您的设置器中添加删除 datagridPisma.DataContext=DataGridContext

    【讨论】:

    • 现在我的数据网格是空的,什么都没有。
    • 现在分享您的 xaml。你应该有类似ItemsSource={Binding DataGridContext}
    • 我按照你说的添加 ItemsSource 但也没有帮助我有错误“找不到类型或命名空间名称 NotifyPropertyChangedInvocator”
    • 然后删除[NotifyPropertyChangedInvocator]属性
    • 请输入AutoGenerateColumns=True 只是为了测试。还有This.DataContext=this
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多