【问题标题】:Binding in GroupStyle header not updated when collection changes集合更改时未更新 GroupStyle 标头中的绑定
【发布时间】:2016-07-07 04:44:42
【问题描述】:

我有一个ItemsControl,它绑定到一个绑定到视图模型属性的CollectionViewSource

ItemsControl 有一个 GroupStyle 集,如下所示:

<GroupStyle HeaderTemplate="{StaticResource TotalDurationTemplate}" />

TotalDurationTemplate 在哪里:

<DataTemplate x:Key="TotalDurationTemplate">
    <Border BorderBrush="Black" BorderThickness="0 1" Background="#EEE">
        <Grid>
            <TextBlock HorizontalAlignment="Center"
                                           FontSize="18" FontWeight="Bold"
                                           Text="{Binding Path=Items[0].Start, Converter={StaticResource DateTimeFormatConverter}, ConverterParameter='ddd dd/MM'}" />
            <TextBlock Margin="10 0" HorizontalAlignment="Right" VerticalAlignment="Center"
                                           FontSize="16" Foreground="#9000"
                                           Text="{Binding Items, Converter={StaticResource TotalDurationConverter}}" />
        </Grid>
    </Border>
</DataTemplate>

问题是第二个TextBlock(绑定到Items)在将新项目添加到视图模型的集合(即ObservableCollection&lt;&gt;)时不会重新评估。该项目已添加到 ListView 到正确的组中,但 Total Duration 值未更新。

总时长转换器如下所示:

public class TotalDurationConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return
            ((IEnumerable<object>)value)
                .Select(x => ((RecentTimingViewModel)x).Duration)
                .Aggregate((v1, v2) => v1 + v2)
                .TotalHours
                .ToString("F2") + "h";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new InvalidOperationException();
    }
}

当视图模型中的项目发生变化时,如何使绑定正确刷新?

编辑:解决方案

我从接受的答案中提取了解决方案 2,并将其放入我的代码中。这就是最终的工作:

<DataTemplate x:Key="TotalDurationTemplate">
    <Border BorderBrush="Black" BorderThickness="0 1" Background="#EEE">
        <Grid>
            <TextBlock HorizontalAlignment="Center"
                       FontSize="18" FontWeight="Bold"
                       Text="{Binding Path=Items[0].Start, Converter={StaticResource FormatDateIntelligentConverter}}" />
            <TextBlock Margin="10 0" HorizontalAlignment="Right" VerticalAlignment="Center"
                       FontSize="16" Foreground="#9000">
                <TextBlock.Text>
                    <MultiBinding Converter="{StaticResource TotalDurationConverter}">
                        <MultiBinding.Bindings>
                            <Binding Path="Items" />
                            <Binding Path="Items.Count" />
                        </MultiBinding.Bindings>
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </Grid>
    </Border>
</DataTemplate>

并将TotalDurationConverter 更改为IMultiValueConverter。只需忽略Array 中的第二项即可。

【问题讨论】:

    标签: c# wpf mvvm groupstyle


    【解决方案1】:

    所以有两种可能性,如果您可以尝试以下简单的解决方案,请告诉我它是否有效。

    解决方案 1 - 一个非常简单和基本的解决方案,因为您使用的是 textbloxk,将模式明确设置为双向。我猜 TextBlock 默认绑定模式是一种方式。

    解决方案 2 - 我在使用组合框时遇到了类似的问题 - 这是一个对我有用的解决方法 对于第二个文本块使用多重绑定,首先将其绑定到您已经完成的列表,然后将其绑定到视图模型中的任何属性,当您的列表发生更改时将触发该属性(例如返回 List.Count 的 int 属性) -第二个虚拟属性将确保您的转换器被重新评估。

    我想第二个选项应该适合你。

    如果它不起作用,请告诉我。

    问候, 维沙尔

    【讨论】:

    • 是的,下班后我想在 MultiBinding 中绑定到源列表的 List.Count。明天试试,看看效果如何。
    • 酷,它在其中一种情况下对我有用......让我知道它是否有效。一切顺利:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 2010-11-20
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    相关资源
    最近更新 更多