[英]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.