微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WPF ObservableCollection & InotifyPropertyChanged

如何解决WPF ObservableCollection & InotifyPropertyChanged

模型类代表数据库。我从视图模型中的数据库提取数据并将其显示在数据网格中。当我更改数据网格中的相应单元格时,此更改不会反映在 ObservableCollection 对象中。旧值来了。

型号

namespace ptkym.Models
{
   using ptkym.Helpers;
   public class M_PKY_Rapor_Sablon_Ek_1_Planlar : ObservableObject
   {
      string document_No;       
      public string Document_No
      {
        get { return document_No; }
        set { document_No = value; OnPropertyChanged(); }
       }       
       public M_PKY_Rapor_Sablon_Ek_1_Planlar(string document_No)                                              
       {
            this.Document_No = document_No;            
       } 
       }
      }

视图模型

        namespace ptkym.viewmodels
        {
          using ptkym.Helpers;
          using ptkym.Models;
          using System;
          using System.Collections.ObjectModel;
          using System.Data;
          using System.Linq;
          using System.Windows;
          public class viewmodel
          {
          sql_class sql = new sql_class();
          public RelayCommand displayMessageCommand { get; private set; }

          private ObservableCollection<Model> obsColl;
          public ObservableCollection<Model> OBSCOLL
           {
                get { return obsColl; }
                set { obsColl = value; }
            }

           public viewmodel()
           {
               obsColl = new ObservableCollection<Model>();
               this.Fill_DataGrid();
                displayMessageCommand = new RelayCommand(displayMessage);
            }
              private void Fill_DataGrid()
              {
                 sql.QueryString = "SELECT * FROM sql_Table_Name";
                 DataTable dataTable = new DataTable();
                 dataTable = sql._DataTable();

               foreach (DaTarow dr in dataTable.Rows)
               {
                 obsColl.Add(new Model(dr["Dokuman_No"].ToString())                                  
                }
          }
            public void displayMessage(object message)
            {
                MessageBox.Show(Sablon_Ek_1_Planlar[0].Document_No);
             }
         }
       }

INotifyPropertyChanged 实现

      namespace ptkym.Helpers
      {
        using System.ComponentModel;
        using System.Runtime.CompilerServices;
        public class ObservableObject : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
               PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
            }
         }
      }

查看

        <Window x:Class="ptkym.Views.PKY_Sablon_Ek_1_Planlar"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:ptkym.Views"
            xmlns:lokal="clr-namespace:ptkym.viewmodels"
            mc:Ignorable="d"
           Title="PKY_Sablon_Ek_1_Planlar" Height="512.534" Width="1196.049">

        <Window.DataContext>
             <lokal:viewmodel></lokal:viewmodel>
        </Window.DataContext>


         <Grid Margin="0,32,0">
          <Grid.ColumnDeFinitions>
         <ColumnDeFinition Width="331*"/>
        <ColumnDeFinition Width="65*"/>
           </Grid.ColumnDeFinitions>
           <DataGrid x:Name="Grd" CanUserAddRows="True" ItemsSource="{Binding Sablon_Ek_1_Planlar,Mode=TwoWay}" AutoGenerateColumns="False" HorizontalAlignment="Left" Height="369" 
         Margin="79,42,0" VerticalAlignment="Top" Width="1053" Grid.ColumnSpan="2">
        <DataGrid.Columns>

            <!--DOCUMENT NO-->
            <DataGridTemplateColumn Header="NO">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Document_No,Mode=TwoWay}"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>
    </DataGrid>
    <Button Content="Button" Command="{Binding displayMessageCommand}" HorizontalAlignment="Left" 
      Margin="204,440,0" VerticalAlignment="Top" Width="75"/>
        </Grid>
       </Window>

解决方法

OBSCOLL 需要调用 OnPropertyChanged();在它的 setter 中也是如此。

然后,在您的视图模型构造函数中更改为:

OBSCOLL = new ObservableCollection<Model>();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。