【发布时间】:2015-05-28 12:28:17
【问题描述】:
我正在使用 WPF DataGrid 来显示已分组为两级深度的数据。大多数 XAML/代码都基于 https://msdn.microsoft.com/en-us/library/ff407126%28v=vs.110%29.aspx 上的 MSDN 示例。
除了这个示例之外,我还在每个二级分组下方添加了一个汇总行来进行一些汇总。用户能够更改影响一个或多个总和的行。每次用户进行更改时,都应直接重新评估总和,以便查看更改的结果。
除了汇总行更新之外,所有功能都可以正常工作。用户更新网格中的字段后,不会重新评估总和。实际上,求和的绑定不会触发调用转换器进行新的求和。我发现通过单击鼠标将焦点更改到 DataGrid 的另一部分(更改的行之外!)时,触发器确实出现了。
让我澄清一下我的 XAML/代码的相关部分...
为了获取属于该组的行,我绑定到 Items 属性并将它们发送到转换器。在 XAML 中,此摘要行显示在元素的元素中:
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="1*" />
<RowDefinition Height="20" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Text="{Binding Path=Name}"
FontWeight="Bold" FontSize="13"
TextAlignment="Left" Margin="5,0,0,0"/>
<ItemsPresenter Grid.Row="1" />
<!-- Grid that represents the summary row -->
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- for simplicity, only one summation field-->
<!-- This binding isn't updated unless I change focus somewhere outside the edited row -->
<TextBlock Grid.Column="0" Margin="0,0,-2,0" HorizontalAlignment="Right" FontWeight="Bold" Text="{Binding Path=Items,
Converter={StaticResource decimalGroupSumConverter}, ConverterParameter=AmountPerYear}"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
求和调用的转换器:
public class DecimalGroupSumConverter : IValueConverter
{
public object Convert(object value, System.Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
if (null == value)
return "null";
ReadOnlyObservableCollection<object> items =
(ReadOnlyObservableCollection<object>)value;
if (parameter.ToString() == "AmountPerYear")
{
var sum = (from i in items
select ((ObservableCalculatedActivity)i).AmountPerYear).Sum();
return string.Format("{0:c}", sum);
}
else
{
return null;
}
}
public object ConvertBack(object value, System.Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
throw new System.NotImplementedException();
}
}
下一个列表显示了我尝试强制更新 UI 但没有任何成功:
- 通过删除并重新添加已更改的行来更改基础 sourcecollection
- 在视图的 ICollectionView 接口上调用 Refresh() 方法。
- 重新排序集合
- 根据此链接刷新数据网格:http://geekswithblogs.net/NewThingsILearned/archive/2008/08/25/refresh--update-wpf-controls.aspx
拜托,有人可以帮我解决这个关于更新 UI 的小但非常烦人的问题吗?
【问题讨论】:
-
“Items”属性是否实现了 INotifyPropertyChanged 接口?