如何在集合对象

时间:2018-01-29 19:22:07

标签: c# inotifypropertychanged

我有一个类,我通常按如下方式定义一个属性:

public class MeasurementPoint : ModelBase
{
    private double _value;
    public double Value
    {
        get { return _value; }
        set
        {
            _value = value;
            NotifyPropertyChanged();
        }
    }
}

接下来,我创建了一个包含许多'MeasurementPoint'对象的集合。我想在每个符合特定值逻辑的对象上引发notifyPropertyChanged。

此方法有效,并且会引发propertyChanged。但是,肯定有更有效的方法可以做到这一点吗?

private void RefreshDataGridTolerance()
{
    foreach (var measurementPoint in DataSet)
    {
        //TODO: Change this into a real way to raiseproperty changed without actually changing the value
        var temp = measurementPoint.Value;
        measurementPoint.Value = temp;

        // something like this doesnt work?
        // RaisePropertyChanged(nameof(measurementPoint.Value));
    }
}

该集合定义如下:ObservableCollection<MeasurementPoint> DataSet

1 个答案:

答案 0 :(得分:3)

您可以在模型类中实现一个公共方法,该方法将为每个已定义的属性引发属性更改事件。最便宜的方法是:

var element = DataSet.First();
element.RefreshAllProperties();

你可以像这样刷新一个数据元素的绑定。

SELECT
    CASE
        WHEN Total BETWEEN 1 AND 2 THEN '1-2 Comments'
        WHEN Total BETWEEN 3 AND 4 THEN '3-4 Comments'
        WHEN Total >= 5 THEN '5+ Comments'
    END AS Range,
    COUNT(*) AS `Count`
FROM (
    SELECT
        id,
        SUM(CASE Attendance WHEN LENGTH(Attendence) > 0 OR Attendence IS NOT NULL THEN 1 ELSE 0 END CASE) + SUM(CASE Accountability WHEN LENGTH(Accountability) > 0 OR Accountability IS NOT NULL THEN 1 ELSE 0 END CASE) + SUM(CASE Respect WHEN LENGTH(Respect) > 0 OR Respect IS NOT NULL THEN 1 ELSE 0 END CASE) AS Total
    FROM
        MainTable
    GROUP BY
        id
) AS t
GROUP BY
    CASE
        WHEN Total BETWEEN 1 AND 2 THEN '1-2 Comments'
        WHEN Total BETWEEN 3 AND 4 THEN '3-4 Comments'
        WHEN Total >= 5 THEN '5+ Comments'
    END