WPF Datagrid 分组 - 具有绑定转换器的项目未动态更新

发布于 2024-11-17 04:51:12 字数 1825 浏览 2 评论 0原文

我是 WPF 新手,使用 datagrid 控件对项目进行分组,数据源自 java 消息 Q,项目在到达时动态添加。

我使用 GroupStyle 显示分组名称、项目计数和读取显示执行数量的自定义转换器。

我遇到的问题是,项目计数会在项目到达时更新,但不会调用自定义转换器类。

<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 类中:

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

在上面的转换器中,当选项卡加载时,如果有 2 个项目在从 MQ 读取时依次加载,则“订单”计数为 1。

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

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

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒗幽 2024-11-24 04:51:12

就我个人而言,我已经放弃了一次性转换器,转而将转换逻辑放入 ViewModel 的属性中。但是,如果预计要在多个不同的视图/视图模型之间使用转换,那么我会将转换逻辑放入专用转换器中。

我想您已经在转换器中设置了一个断点来查看它何时被调用以及它的输出是什么?

如果是这样,我有兴趣知道它何时被调用及其输出。否则,这将是我采取的第一步。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文