0

我有一个绑定到已定义类型的 ObservableCollection 的 Telerik RadGridView。在更新该类型中特定属性的值时,相应的 RadGridView 单元格正在更新,但页脚中的总数没有更新。我已经创建了相同的依赖属性,但出现错误:设置属性“System.Windows.Setter.Property”引发异常。

下面是 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.Resources>
                    <Style TargetType="{x:Type telerik:RadGridView}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.UpdateFooter}" Value="True">
                                <Setter Property="my:UpdateGridViewFooter.HandleGridViewUpdateFooterProperty" Value="True" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </telerik:RadGridView.Resources>
 <telerik:RadGridView.Columns>
  <telerik:GridViewDataColumn Header="Quantity" DataMemberBinding="{Binding Path=Quantity}">
   <telerik:GridViewColumn.AggregateFunctions>
                            <telerikData:SumFunction SourceField="Weight"/>
                        </telerik:GridViewColumn.AggregateFunctions>
   </telerik:GridViewDataColumn>
  </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();
            }
        }
    }

   public bool UpdateFooter { get; set; }

   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);
        });
    }
    this.UpdateFooter = true; 
}

对象等级:

public class Result{
   private decimal weight;
   public decimal Weight { 
     get { return this.weight;} 
     set { this.weight = value;
     ((IHaveOnPropertyChangedMethod) this).OnPropertyChanged("Weight");
     }
  }
}

依赖属性:

public static class UpdateGridViewFooter {
    /// <summary>
    /// Handle GridView Footer Update Attached Dependency Property
    /// </summary>
    public static readonly DependencyProperty HandleGridViewUpdateFooterProperty =
        DependencyProperty.RegisterAttached(
            "HandleGridViewUpdateFooter",
            typeof(bool),
            typeof(UpdateGridViewFooter),
            new FrameworkPropertyMetadata(false, new PropertyChangedCallback(UpdateFooterOnValueChanged)));

    /// <summary>
    /// Gets the GridView Footer Update property.  
    /// </summary>
    /// <param name="d">The DependencyObject</param>
    /// <returns>The bool value</returns>
    public static bool GetHandleGridViewUpdateFooterProperty(DependencyObject d) {
        return (bool) d.GetValue(HandleGridViewUpdateFooterProperty);
    }

    /// <summary>
    /// Sets the GridView Footer Update property. 
    /// </summary>
    /// <param name="d">The DependencyObject</param>
    /// <param name="value">The bool value</param>
    public static void SetHandleGridViewUpdateFooterProperty(DependencyObject d, bool value) {
        d.SetValue(HandleGridViewUpdateFooterProperty, value);
    }

    public static void UpdateFooterOnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        var radGridView = d as RadGridView;

        if (radGridView != null) {
            radGridView.CalculateAggregates();
        }
    }
}
4

1 回答 1

1

我今天遇到了同样的问题,即更改时的总数RadGridView没有更新。ViewModel

相反,我实现了一个事件ViewModel,例如ResultChanged,并在调用的视图后面的代码中添加了一个事件侦听器CalculateAggregates()。不是一个非常优雅的解决方案,但它有效。

于 2011-05-24T17:24:42.233 回答