简体   繁体   English

集合更改时,GroupStyle标头中的绑定不会更新

[英]Binding in GroupStyle header not updated when collection changes

I have an ItemsControl which is bound to a CollectionViewSource bound to a property on the View Model. 我有一个ItemsControl绑定到绑定到View Model上的属性的CollectionViewSource

The ItemsControl has a GroupStyle set which looks like this: ItemsControl有一个GroupStyle集,如下所示:

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

Where TotalDurationTemplate is: 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>

The issue is that the second TextBlock (the one bound to Items ) is not re-evaluated when a new item is added to the View Model's collection (which is an ObservableCollection<> ). 问题是当一个新项添加到View Model的集合(它是一个ObservableCollection<> )时,不会重新评估第二个TextBlock (绑定到Items的那个)。 The item is added to the ListView into the correct group but the Total Duration value is not updated. 该项目将添加到ListView中正确的组,但不会更新总持续时间值。

The Converter for Total Duration looks like this: 转换器总持续时间如下所示:

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

How do I make the binding refresh correctly when the items in the View Model are changed? 当视图模型中的项目发生更改时,如何正确刷新绑定?

EDIT: The Solution 编辑:解决方案

I took Solution 2 from the Accepted Answer and put it into my code. 我从接受的答案中选择了解决方案2并将其放入我的代码中。 This is what ended up working: 这就是最终的工作:

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

And changing TotalDurationConverter to IMultiValueConverter . 并将TotalDurationConverter更改为IMultiValueConverter The just ignore the second item in the Array . 只是忽略Array的第二项。

So two possibilities and if you can try below simple solutions and let me know if it works. 所以有两种可能性,如果你可以尝试以下简单的解决方案,让我知道它是否有效。

Solution 1 - a very simple and basic one since you are using a textbloxk set the mode explicitly to Two way. 解决方案1 - 一个非常简单和基本的解决方案 ,因为您使用textbloxk将模式明确设置为双向。 I guess TextBlock default binding mode is One way. 我猜TextBlock默认绑定模式是单向的。

Solution 2 - I have faced similar issue working with a combo box- here is a work around which worked for me For the second Text block use Multi Binding, first bind it to List as you have already done, second bind it to any property in View Model which will be triggered when your list is getting changed (example an int property returning List.Count) - This second dummy property will make sure that your converter is re evaluated. 解决方案2 - 我在使用组合框时遇到了类似的问题 - 这是一个为我工作的工作对于第二个文本块使用多重绑定,首先将它绑定到List已经完成,然后将其绑定到任何属性查看模型将在列表更改时触发(例如,返回List.Count的int属性) - 第二个虚拟属性将确保重新评估转换器。

I guess second option should work for you. 我猜第二个选项对你有用。

Let me know if it doesn't work. 如果它不起作用,请告诉我。

regards, Vishal 问候,维沙尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Silverlight:绑定更改时,ComboboxItem文本未更新 - Silverlight: ComboboxItem text not updated when binding changes 取消EF上下文更改后,为什么不更新集合? - Why a collection is not updated when the EF context changes are cancelled? 绑定ListBox.GroupStyle.ContainerStyle - Binding ListBox.GroupStyle.ContainerStyle WPF绑定:在绑定集合中项目的属性更改时做出反应 - WPF Binding: Reacting when an item's property changes in a bound collection GroupStyle 和 Expander.IsExpanded 绑定的问题 - Problem with GroupStyle and Expander.IsExpanded Binding 绑定更改时,重载的UI线程上的进度对话框未更新 - Progress dialog on a heavy loaded UI thread does not get updated when binding changes WPF ListView.GroupStyle不显示组头 - WPF ListView.GroupStyle not displaying group header WPF Datagrid行标题绑定-可观察的集合 - WPF datagrid row header binding - Observable collection 将DataGrid列标题绑定到String或Collection的列表 - Binding of DataGrid column header to a list of String or Collection 当代码在运行时更新时,将 Observable 集合绑定到组合框会起作用。 但是在重新构建解决方案后它不起作用 - Binding an Observable Collection to Combo Box works when code is updated during runtime. But it doesn't work after solution is built again
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM