1

我有一个绑定到已定义类型的 ObservableCollection 的 Telerik RadGridView。在更新该类型中特定属性的值时,相应的 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; }
}
4

3 回答 3

3

Observable Collection 中的每个项目都必须有一种方式向 WPF 发出信号,因此它正在更新的控件,以便可以更新 Quantity Value。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");
        }
    }
}
于 2011-02-19T18:15:01.887 回答
1

正如 Erno 所说,您的 Result 类需要实现 INotifyPropertyChanged 并且您需要调用OnPropertyChangedWeight属性。

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

于 2011-02-19T15:39:03.253 回答
0

如果您所做的更新是对 Result 对象的 Weight 属性的更新,那么这可能是由于 Result 类没有实现 INotifyPropertyChanged 造成的。

ICollectionChanged(因此也是 ObservableCollection)仅通知集合的更改(添加和删除项目)。

于 2011-02-19T15:14:11.680 回答