WPF GridView는 관찰 가능한 컬렉션 변경시 업데이트되지 않습니다

StackOverflow https://stackoverflow.com/questions/5051530

  •  15-11-2019
  •  | 
  •  

문제

정의 된 유형의 ObservableCollection에 바인딩 된 Telerik RadGridView가 있습니다. 해당 유형의 특정 속성 값을 업데이트 할 때 해당 관찰 가능한 컬렉션이 업데이트되지만 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; }
}
.

도움이 되었습니까?

해결책

Each item within the Observable Collection must have a way to signal to WPF and therefore the control that it is updating so that the Quantity Value can be updated. The Observable Collection update is occuring because that collection contains in built change notification for its collection but not its individual items. As others have said implement INotifyPropertyChanged on Result and it should work.

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");
        }
    }
}

다른 팁

As Erno said your Result class needs to implement INotifyPropertyChanged and you need to call OnPropertyChanged for the Weight property.

Also, your ViewModel should also implement INotifyPropertyChanged so you can avoid the casting to IHaveOnPropertyChangedMethod (that naming is pretty bad btw) that you have in your code. I would suggest having a ViewModelBase : INotifyPropertyChanged class that all your ViewModels inherit from. If you don't want to write one yourself there are plenty of MVVM frameworks out there that have this built in.

If the update that you are making is an update to the Weight property of a Result Object then this might be caused by the fact that the Result class is not inmplementing INotifyPropertyChanged.

ICollectionChanged (and thus ObservableCollection too) only notifies changes to the collection (adding and removing items).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top