WPF Datagrid Grouping - items with binding converter not updating dynamically

随声附和 提交于 2020-01-25 23:58:18

问题


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 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>

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

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.

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.

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

any ideas pls?


回答1:


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.

I imagine you have set a breakpoint in the converter to see when it is being called, and what it's output is?

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.

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



来源:https://stackoverflow.com/questions/6452701/wpf-datagrid-grouping-items-with-binding-converter-not-updating-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!