我有一個ItemsControl
綁定到綁定到查看模型上屬性的CollectionViewSource
。綁定GroupStyle標頭未更新收集更改時
的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
一)不重 - 將新項目添加到View Model的集合(這是一個ObservableCollection<>
)時評估。該項目被添加到ListView
到正確的組中,但總持續時間值不會更新。
的總時間轉換看起來是這樣的:
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();
}
}
如何讓我的綁定刷新正確時,在視圖模型的項目被改變了嗎?
編輯:解
我把方案二從接受的答案,並把它放到我的代碼。這是結束了工作:
<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
中的第二項。
呀留下我想結合在一個MultiBinding源列表的List.Count下班後。明天再試,看看它是如何發展的。 –
酷,它在我的一個場景中工作...讓我知道它是否工作。祝一切順利 :) –