尝试在DevExpress GridControl ItemsSource上更新绑定值

时间:2016-04-27 11:06:25

标签: c# wpf data-binding devexpress

我在WPF中有一个带有绑定ItemsSource的DevExpress GridControl和列中的字段。当我初始化数据源中的值时,一切正常,但是当数据应该更新时,它并没有。

我在用户控件中也有一个标签,其中包含GridControl并且更新正常。

所以我的XAML是:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="250" />
        <RowDefinition Height="3*" />
    </Grid.RowDefinitions>

    <dxg:GridControl Grid.Row="0" x:Name="grid" DataContext="{StaticResource ParamDataSource}" ItemsSource="{Binding Path=ObservableParams}">
        <dxg:GridControl.Columns>
            <dxg:GridColumn x:Name="ParamName" FieldName="ParamName" MinWidth="80" Width="80" AllowResizing="False" FixedWidth="True" Header="Parameter" />
            <dxg:GridColumn x:Name="ParamValue" Binding="{Binding ParamValue}" MinWidth="50" Width="50" SortIndex="0" Header="Best Value" />
        </dxg:GridControl.Columns>

        <dxg:GridControl.View>
            <dxg:TableView VerticalScrollbarVisibility="Hidden" x:Name="view" ShowGroupPanel="False" AllowFixedGroups="True" ShowGroupedColumns="False" AllowCascadeUpdate="False" AllowScrollAnimation="False" NavigationStyle="Row" AutoWidth="True" ShowFixedTotalSummary="False" />
        </dxg:GridControl.View>
    </dxg:GridControl>

    <Label DataContext="{StaticResource ParamDataSource}" Content="{Binding LabelText}" Margin="10, 10, 10, 10" Grid.Row="1"/>
</Grid>

然后是数据源的c#代码......

class ParamDataSource : ViewModelBase    // using DevExpress.Mvvm above
{

    public ParamDataSource()
    {

// This stuff is put on the grid no problem. 

        ObservableParams = new System.Collections.ObjectModel.ObservableCollection<ParamTableRow>
            {
                new ParamTableRow
                {
                    ParamName = "Param1", 
                    ParamValue = 0
                },

                new ParamTableRow 
                {
                    ParamName = "Param2",
                    ParamValue = 0
                },

                new ParamTableRow
                {
                    ParamName = "Param3", 
                    BestValue = 0
                }
            }; 

        LabelText = "Starting Now";
    }

    public ObservableCollection<ParamTableRow> ObservableParams { get; set; }

    public string LabelText { get; set; }

    public void UpdateParam(int paramIndex, decimal? paramValue)
    {
        ObservableParams[paramIndex].ParamValue = paramValue;
        RaisePropertyChanged("ObservableParams");

// This label updates on the view just fine, but not the parameter values... 

        LabelText = string.Format("Done Param {0}", paramIndex);
        RaisePropertyChanged("LabelText");
    }
}

public class ParamTableRow
{
    public string ParamName { get; set; }
    public decimal? ParamValue { get; set; }
}

1 个答案:

答案 0 :(得分:1)

在模型类上实现INotifyPropertyChanged

public class ParamTableRow:INotifyPropertyChanged
{
    private string paramName;

    public string ParamName
    {
        get { return paramName; }
        set {
            paramName = value;
            OnPropertyChanged("ParamName");
        }
    }

    private decimal? paramValue;
    public decimal? ParamValue
    {
        get { return paramValue; }
        set
        {
            paramValue = value;
            OnPropertyChanged("ParamValue");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new  PropertyChangedEventArgs(propertyName));
    }        
}

原因ObservableCollection实现INotifyCollectionChanged,而不是INotifyPropertyChanged

INotifyCollectionChanged用于在添加或从集合中删除项目时通知UI。

INotifyPropertyChanged用于在为您的媒体资源设置新值时通知用户界面。

相关问题