WPF GridView未更新可观察的集合更改

时间:2011-02-19 15:05:50

标签: wpf radgridview

我有Telerik RadGridView绑定到已定义类型的ObservableCollection。在更新该类型中特定属性的值时,相应的Observable集合正在更新但不是RadGridView ..

以下是XAML:

<ComboBox Grid.Column="4" Width="100" Margin="4" ItemsSource="{Binding Path=ResultUnits, Mode=OneTime}" SelectedValue="{Binding Path=ResultUnit}"/>

<telerik:RadGridView ItemsSource="{Binding Path=Results, Mode=TwoWay}" FooterRowStyle="{StaticResource GridViewFooterStyle}" Width="{Binding RelativeSource={RelativeSource AncestorType= ScrollViewer}, Path=ActualWidth}" ColumnWidth="*" RowIndicatorVisibility="Collapsed" EditTriggers="None" IsFilteringAllowed="False" AutoGenerateColumns="False" AllowDrop="False" CanUserFreezeColumns="False" CanUserReorderColumns="False" CanUserDeleteRows="False" CanUserInsertRows="False" ShowGroupPanel="False" ShowColumnFooters="True">
 <telerik:RadGridView.Columns>
  <telerik:GridViewDataColumn Header="Quantity" DataMemberBinding="{Binding Path=Quantity}"/>
  </telerik:RadGridView.Columns>
</telerik:RadGridView>

以下是查看型号代码:

public class ViewModel {

    private const decimal PoundsToKilograms = 0.45359237M;

    private const decimal GramstoPound = 0.00220462262M;

    private ObservableCollection<Result> results;

    public EnvironmentalSummaryViewModel() {
        this.results= new ObservableCollection<Result>();
    }

    public ObservableCollection<Result> Results{
        get {
            return this.results;
        }

        set {
            this.results = value;
        }
    }

  public string ResultUnit {
        get {
            return this.resultUnit;
        }

        set {
            if (this.resultUnit != value) {
                this.resultUnit = value;
                this.UpdateGridViewValuesOnResultUnitChanged();
            }
        }
    }

   private void UpdateGridViewValuesOnResultUnitChanged() {
        bool isEnglish = this.resultUnit == this.resultUnits[0];
        this.results.ToList().ForEach(result => {
            decimal weight = isEnglish ? result.Weight * GramstoPound * 1000 : environmental.Weight * PoundsToKilograms;
            result.Weight = Math.Round(weight, 2);
        });

        ((IHaveOnPropertyChangedMethod) this).OnPropertyChanged("Results");
    }
}

对象类:

public class Result{
   public decimal Weight { get; set; }
}

3 个答案:

答案 0 :(得分:3)

Observable Collection中的每个项目都必须有一种方式向WPF发出信号,因此控制它正在更新,以便可以更新数量值。 Observable Collection更新正在发生,因为该集合包含其集合的内置更改通知,但不包含其各个项目。正如其他人所说,在Result上实现INotifyPropertyChanged它应该可行。

public class Result : INotifyPropertyChanged
{

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    protected void Notify(string propName)
    {
        if (this.PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
    #endregion


    private decimal _weight;

    public decimal Weight
    {
        get { return _weight; }
        set { 

            this._weight = value;
            Notify("Weight");
        }
    }
}

答案 1 :(得分:1)

正如Erno所说,你的Result类需要实现INotifyPropertyChanged,你需要为OnPropertyChanged属性调用Weight

此外,您的ViewModel还应该实现INotifyPropertyChanged,这样您就可以避免在代码中转换为IHaveOnPropertyChangedMethod(命名非常糟糕)。我建议你有一个ViewModelBase : INotifyPropertyChanged类,你的所有ViewModel都会从中继承。如果您不想自己编写一个,那么有很多内置的MVVM框架。

答案 2 :(得分:0)

如果您正在进行的更新是对Result对象的Weight属性的更新,那么这可能是由于Result类未实现INotifyPropertyChanged这一事实引起的。

ICollectionChanged(以及ObservableCollection)仅通知对集合的更改(添加和删除项目)。