WPF中的DataGrid 数据动态刷新UI

动态刷新Colume4的值

1:原数据定义

数据类继承: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");

            }

        }

2:ModeView 定义ObservableCollection

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);

            }

        }

}


3:View 绑定数据源xaml

<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>

看到另一种方法

图片发自简书App
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容