WPF Datagrid分组-具有绑定转换器的项目不会动态更新 [英] WPF Datagrid Grouping - items with binding converter not updating dynamically

查看:131
本文介绍了WPF Datagrid分组-具有绑定转换器的项目不会动态更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是WPF的新手,它使用datagrid控件对项目进行分组,数据来自Java消息Q,并且在项目到达时动态添加。

Am new to WPF and using the datagrid control to group items, data is sourced from a java message Q and items are added dynamically as they arrive.

Am使用GroupStyle来显示分组名称,项目数和读取的自定义转换器将显示已执行的数量。

Am using GroupStyle to display the Grouping name, Items Count and a custom converter that reads displays the executed quantity.

我的问题是,项目数在到达时会更新

The issue I have is that the item count updates as and when items arrive but the custom converter class is not invoked.

<DataGrid.GroupStyle>
    ...
 <TextBlock.Text>
        <MultiBinding StringFormat="{}{0}, Count: {1:#0}, Executed Qty: {2}">
            <Binding Path="Name" />
            <Binding Path="ItemCount" />
            <Binding Path="Items" Converter="{StaticResource convertToExecutedQty}" />
        </MultiBinding>
</TextBlock.Text>
.....
</DataGrid.GroupStyle>

在Converter类中:

And in the Converter class:

public class ConvertToExecutedQty : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null && value is IEnumerable<Object>)
        {
            IEnumerable<TickOrderViewModel> orders = ((IEnumerable<object>) value).GetBaseElements<TickOrderViewModel>();


            if (orders.Count() > 0)
            {
                var BuyQty = ( from n
                              in orders
                               where n.side.ToUpperInvariant() == "BUY"
                               select n.executed_qty ).Sum();

                var SellQty = ( from n
                              in orders
                               where n.side.ToUpperInvariant() == "SELL"
                               select n.executed_qty ).Sum();

                return BuyQty - SellQty;
            }
        }

        return null;
    }

在上述转换器中,当选项卡加载时,订单计数为1

In the above converter, when the tab loads, "orders" count is 1 when there are 2 items that were loaded one after the other as they were read from the MQ.

例如:我有2行,其中SELL为1,SELL为BUY 1
最初加载时,收藏仅看到1行,并且执行的数量显示-3。

Ex: I have 2 rows with SELL as 1 and SELL as BUY as 1 Initially on load the collection sees only 1 row, and the executed qty displays -3.

当我切换标签时,订单现在有2项,并且Exe数量变为-2的正确值

When i switch tabs, then orders now have 2 items and the Exe qty becomes the correct value of -2

有任何想法吗?

推荐答案

我个人不再使用一次性转换器,而是将转换逻辑放入ViewModel的属性中。但是,如果希望在多个不同的View / ViewModel之间使用转换,那么我会将转换逻辑放在专用的转换器中。

Personally I have done away with single use converters in favor of putting conversion logic in the properties in my ViewModel. If however a conversion is expected to be used across several different Views/ViewModels then I put the conversion logic in a dedicated converter.

我想您已经在

如果是这样,我很想知道何时调用它以及它的输出。否则,这将是我要采取的第一步。

If so, I would be interested to know when it is called and it's output. Otherwise, that would be the first step I would take.

根据我的经验,Converter经常遇到VM Properties不会遇到的问题。

It has been my experience that Converters often experience problems that VM Properties do not.

这篇关于WPF Datagrid分组-具有绑定转换器的项目不会动态更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆