Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 集合更改时未更新GroupStyle标头中的绑定_C#_Wpf_Mvvm_Groupstyle - Fatal编程技术网

C# 集合更改时未更新GroupStyle标头中的绑定

C# 集合更改时未更新GroupStyle标头中的绑定,c#,wpf,mvvm,groupstyle,C#,Wpf,Mvvm,Groupstyle,我有一个ItemsControl,它绑定到一个CollectionViewSource绑定到视图模型上的属性 ItemsControl有一个GroupStyle集合,如下所示: <GroupStyle HeaderTemplate="{StaticResource TotalDurationTemplate}" /> public class TotalDurationConverter : IValueConverter { public object Convert(o

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

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

<GroupStyle HeaderTemplate="{StaticResource TotalDurationTemplate}" />
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();
    }
}
问题在于,在视图模型的集合(这是一个
可观察集合
)中添加新项时,第二个
文本块
(绑定到
)不会重新计算。该项目将添加到
列表视图中的正确组中,但总持续时间值不会更新

总持续时间的转换器如下所示:

<GroupStyle HeaderTemplate="{StaticResource TotalDurationTemplate}" />
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();
    }
}
公共类TotalDurationConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
返回
((IEnumerable)值)
.选择(x=>((RecentTimeingViewModel)x).持续时间)
.聚合((v1,v2)=>v1+v2)
.总小时数
.ToString(“F2”)+“h”;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
抛出新的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
。只需忽略
数组中的第二项

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

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

解决方案2-我在使用组合框时也遇到过类似的问题-以下是一个适合我的解决方案 对于第二个文本块使用多重绑定,首先将其绑定到列表,就像您已经做的那样,然后将其绑定到视图模型中的任何属性,当列表发生更改时将触发该属性(例如,返回List.Count的int属性)-第二个伪属性将确保重新计算转换器

我想第二种选择应该对你有用

如果不起作用,请告诉我

问候,,
Vishal

是的,下班后我想到了绑定到列表。在多重绑定中计算源列表。我明天会试试,看看效果如何。酷,在其中一个场景中它对我起了作用。。。让我知道它是否有效。祝你一切顺利:)