如何刷新Devexpress gridcontrolitemssource(作为Datatable)在MVVM(WPF)

本文关键字:Datatable MVVM WPF 作为 Devexpress 何刷新 刷新 gridcontrolitemssource | 更新日期: 2023-09-27 18:14:52

我有一个GridControl(Devexpress v13)在视图(WPF)。Datatable在ViewModel中设置值并分配给ItemsSource。但是ItemsSource填充只是初始化。后来Datatable的值改变了,但它不刷新。如何刷新ItemsSource ?

<dxg:GridControl Name="GridControlData" DataSource="{Binding DtCriterias, Mode=TwoWay}" HorizontalAlignment="Left" VerticalAlignment="Top" AutoGenerateColumns="AddNew" Width="400" Height="100">
我希望你明白我的意思。如有任何帮助,我将不胜感激。

提前感谢。

编辑:

使用

更改属性
 public DataTable DtCriterias {
            get { return _dtCriterias; }
            set
            {
                _dtCriterias = value;
                Notify(() => DtCriterias);
            }
        }
protected void Notify(Expression<Func<object>> expression)
        {
            if (_propertyChangedEvent == null) return;
            Notify(GetPropertyName(expression));
        }
protected void Notify(string propertyName)
        {
            if (_propertyChangedEvent != null)
            {
                _propertyChangedEvent(this, new PropertyChangedEventArgs(propertyName));
            }
        }

如何刷新Devexpress gridcontrolitemssource(作为Datatable)在MVVM(WPF)

public ObservableCollection<ClientB2B> Clients
{
    get
    {
        return _clients;
    }
    set
    {
        if (_clients == value) return;
        _clients = value;
        OnPropertyChanged(); // This is what you need
    }
}

实现这个接口- INotifyPropertyChanged

protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
        handler(this, e);
}
protected void OnPropertyChanged(string propertyName)
{
    OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
我找到了问题的原因。我正在使用封装,并设置了私有值(_dtCriterias)。因此,Property Changed Event不能工作。

定义:

private DataTable _dtCriterias;
public DataTable DtCriterias {
    get { return _dtCriterias; }
    set
    {
        _dtCriterias = value;
        Notify(() => DtCriterias);
    }
}

当我有问题的数据表集:

_dtCriterias = GetValue().DefaultView.ToTable("FooTable");

解决方案:

DtCriterias = GetValue().DefaultView.ToTable("FooTable");