c# - WPF 可能与 where 子句绑定(bind)?

标签 c# wpf linq xaml data-binding

如果我有一个像这样的 Observable 集合:

public ObservableCollection<SpecialPriceRow> SpecialPriceRows = new ObservableCollection<SpecialPriceRow>();

SpecialPriceRow 类:

public class SpecialPriceRow : INotifyPropertyChanged
{
    public enum ChangeStatus
    {
        Original,
        Added,
        ToDelete,
        Edited
    }

    public ChangeStatus Status { get; set; }
    public string PartNo { get; set; }

    private decimal _price;
    public decimal Price
    {
        get
        {
            return _price;
        }
        set
        {
            if (value != _price)
            {
                _price = value;
                Status = ChangeStatus.Edited;
                OnPropertyChanged("Status");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

我是否可以将 XAML 中的标签绑定(bind)到说...已添加的对象计数?所以我可以得到这样的东西:

enter image description here

其中绿色是集合中“已添加”对象的数量。我将如何着手做这样的事情?

最佳答案

我已经编写了一个 ViewModel,它将执行您正在寻找的所需功能。

    class VM : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public ObservableCollection<SpecialPriceRow> _SpecialPriceRows = new ObservableCollection<SpecialPriceRow>();
        private int _Original = 0;
        private int _Added = 0;
        private int _ToDelete = 0;
        private int _Edited = 0;

        public VM()
        {
            PropertyChanged = new PropertyChangedEventHandler(VM_PropertyChanged);

            //The following lines in the constructor just initialize the SpecialPriceRows.
            //The important thing to take away from this is setting the PropertyChangedEventHandler to point to the UpdateStatuses() function.
            for (int i = 0; i < 12; i++)
            {
                SpecialPriceRow s = new SpecialPriceRow();
                s.PropertyChanged += new PropertyChangedEventHandler(SpecialPriceRow_PropertyChanged);
                SpecialPriceRows.Add(s);
            }
            for (int j = 0; j < 12; j+=2)
                SpecialPriceRows[j].Price = 20;
        }

        private void VM_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
        }

        private void SpecialPriceRow_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Status")
                UpdateStatuses();
        }

        public ObservableCollection<SpecialPriceRow> SpecialPriceRows
        {
            get
            {
                return _SpecialPriceRows;
            }
        }

        private void UpdateStatuses()
        {
            int original = 0, added = 0, todelete = 0, edited = 0;
            foreach (SpecialPriceRow SPR in SpecialPriceRows)
            {
                switch (SPR.Status)
                {
                    case SpecialPriceRow.ChangeStatus.Original:
                        original++;
                        break;
                    case SpecialPriceRow.ChangeStatus.Added:
                        added++;
                        break;
                    case SpecialPriceRow.ChangeStatus.ToDelete:
                        todelete++;
                        break;
                    case SpecialPriceRow.ChangeStatus.Edited:
                        edited++;
                        break;
                    default:
                        break;
                }
            }
            Original = original;
            Added = added;
            ToDelete = todelete;
            Edited = edited;
        }

        public int Original
        {
            get
            {
                return _Original;
            }
            set
            {
                _Original = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Original"));
            }
        }

        public int Added
        {
            get
            {
                return _Added;
            }
            set
            {
                _Added = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Added"));
            }
        }

        public int ToDelete
        {
            get
            {
                return _ToDelete;
            }
            set
            {
                _ToDelete = value;
                PropertyChanged(this, new PropertyChangedEventArgs("ToDelete"));
            }
        }

        public int Edited
        {
            get
            {
                return _Edited;
            }
            set
            {
                _Edited = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Edited"));
            }
        }
    }

注意构造函数中的注释。您需要将每个 SpecialPriceRow 的 PropertyChanged 事件指向 UpdateStatuses 函数才能使其正常工作。 现在您需要做的就是将标签绑定(bind)到 ViewModel 中的适当属性。 如果您的 SpecialPriceRows 列表变得非常大,您可能需要考虑以稍微不同的方式计算状态计数。目前,每次更新一个实例时,它都会遍历整个列表。为了更好地执行此操作,您可能希望在 SpecialPriceRow 类中保留状态的旧值,并且每次发生更新时,增加新状态计数并减少旧状态计数。

关于c# - WPF 可能与 where 子句绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14667649/

相关文章:

c# - 3 层系统中数据库层的正确抽象?

c# - 如何在 C# 中找出句子中的下一个单词?

c# - 如何根据另一个控件调整控件的大小? (WPF)

wpf:当文本对于 1 行来说太大时,使文本 block 高度扩展

linq - 有没有办法让 Linq-to-entities 评估本地表达式

linq - 将 Foreach 循环转换为 Linq 并出现错误

c# - 使用 Xunit 运行 Specflow 但出现错误 nunit.framework

c# - 我可以在 VSIX 中检测到使用 Visual Studio Workspace 保存(未更改)的文档吗?

c# - 单击事件的 GridView 列标题

mysql - linq mysql : select multiple column and send the to view