WPF中的DataGrid 数据动态刷新UI

如题所述

第1个回答  2022-07-02
数据类继承:INotifyPropertyChanged, 实现INotifyPropertyChanged接口

public class CustomTableColumes: INotifyPropertyChanged

{

public event PropertyChangedEventHandler PropertyChanged;

public void NotiFy(string property)

{

    if (PropertyChanged != null)

    {

        PropertyChanged(this, new PropertyChangedEventArgs(property));

    }

}

    //实时刷新行里的某一列调用NotiFy

      public string Colume4

        {

            get

            {

                return _Colume4;

            }

            set

            {

                _Colume4 = value;

              NotiFy("Colume4");

            }

        }

public class PlcDebugViewModel : GenericViewModel<XmlItemNew>

{

private ObservableCollection<CustomTableColumes> customTable = new ObservableCollection<CustomTableColumes>();

      public ObservableCollection<CustomTableColumes> CustomTable

        {

            get

            {

                return customTable;

            }

            set

            {

                this.customTable = value;

                this.RaisePropertyChanged(() => this.CustomTable);

            }

        }

}

<DataGrid  Name="Mygrid" Grid.Row="2" Grid.Column="1" Grid.RowSpan="24" Margin="5"  SelectedCellsChanged="DataRowSelected"  ItemsSource="{Binding CustomTable}" >

                            <DataGrid.Columns>

                                <DataGridTextColumn Width="130*"  Header="名称"  Binding="{Binding Colume0}"  IsReadOnly="True"/>

                                <DataGridTextColumn Width="60*"  Header="读写属性"  Binding="{Binding Colume1}"  IsReadOnly="True"/>

                                <DataGridTextColumn Width="90*"  Header="数值范围"  Binding="{Binding Colume2}"  IsReadOnly="True"/>

                                <DataGridTextColumn Width="100*"  Header="内控参数"  Binding="{Binding Colume3}"  IsReadOnly="True"/>

                                <DataGridTextColumn Width="100*"  Header="当前内容"  Binding="{Binding Colume4}"  IsReadOnly="True"/>

                                <DataGridTextColumn Width="195*"  Header="备注说明"  Binding="{Binding Colume5}"  IsReadOnly="True"/>

                                <DataGridTextColumn Width="30*"  Header=""  Visibility="Hidden"  Binding="{Binding Colume6}"  IsReadOnly="True"/>

                                <DataGridTextColumn Width="30*"  Header=""  Visibility="Hidden"  Binding="{Binding Colume7}"  IsReadOnly="True"/>

                            </DataGrid.Columns>

                        </DataGrid>

看到另一种方法
相似回答