我的 ViewModel observablecollection 中有一定数量的行被分组并显示在数据网格中。
<CollectionViewSource x:Key="ColViewSource" Source="{Binding Collection}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Cat"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
我的行包含一个文本字段和一个三态复选框,该复选框需要为真或不确定状态才能使该行被视为“填充”(默认为假)。我想在组中的总项目中显示组标题中“填充”行的数量。例如,如果我有一个包含 5 个项目的组,并且用户勾选了 2 个复选框,我希望在组标题中看到 2 out of 5 或 2/5。我可以设法在 groupheader 中显示名称和 itemcount,但我坚持显示填充行的数量。这是我希望我的组头看起来的样子。
<Expander.Header>
<StackPanel Orientation="Horizontal" Height="50">
<TextBlock Text="{Binding Name}"/>
implement this-->
<TextBlock Text = amount of items currently filled.
<TextBlock Text="{Binding ItemCount}"/>
</StackPanel>
</Expander.Header>
我已经为我的收藏和项目实现了 propertychanged。我猜它需要某种转换器,有人能指出我正确的方向吗?
最佳答案
根据this article , 您的组样式在 Xaml 中必须如下所示:
<local:PercentageConverterx:Key="percentageConverter" />
//...
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Converter={StaticResource percentageConverter} }"/>
<TextBlock Text="{Binding ItemCount}"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</DataGrid.GroupStyle>
其中
public class PercentageConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
CollectionViewGroup cvg = value as CollectionViewGroup;
int count = 0;
int check = 0;
foreach (Item t in cvg.Items)
{
count++;
if (t.IsCheck== true)
check++;
}
return (check / (double)count).ToString("0.00") + "%";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Binding.DoNothing;
}
}
请注意,我假设 CollectionViewSource.Source 属性设置为 ObservableCollection 其中 Item 是这样的:
public class Item
{
public string Name {get; set; }
public bool? IsChecked { get; set; }
}
它绑定(bind)到三态复选框。
关于c# - 在组标题 WPF 中显示 "filled"行的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42196482/