【问题标题】:Silverlight DataGrid - Customizing Group Header Text with info from items contained withinSilverlight DataGrid - 使用其中包含的项目的信息自定义组标题文本
【发布时间】:2011-03-11 14:24:51
【问题描述】:

我在我的数据网格上重新设置了 DataGridRowGroupHeader 的样式,因为我想更改它显示的文本。 我想要的是它总结它在其组中保存的数据。 (具体来说,代码应该能够检查集合中的所有项目以找到日期最早的项目)。

我在尝试访问集合中的项目时遇到了问题。 我尝试在 RowGroupHeader 中使用转换器来呈现文本 - 但集合总是返回为空。

<sdk:DataGrid.RowGroupHeaderStyles> 
        <Style TargetType="sdk:DataGridRowGroupHeader"> 
            <Setter Property="Template"> 
        <!-- All the other spield from the default template, leading to below where the header text is rendered --> 

        <StackPanel Grid.Column="3" Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center" Margin="0,1,0,1"> 
            <TextBlock x:Name="PropertyNameElement" Margin="4,0,0,0"/> 
            <TextBlock Margin="4,0,0,0" Text="{Binding Name}" /> 
            <TextBlock x:Name="ItemCountElement" Margin="4,0,0,0" Visibility="{TemplateBinding ItemCountVisibility}"/> 
            <TextBlock x:Name="oldest" Margin="4,0,0,0" Text="{Binding Converter={StaticResource myConverter}, ConverterParameter=Orders}" /> 
        </StackPanel>

我的转换器看起来像:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
{ 
    CollectionViewGroup cvg = value as CollectionViewGroup; 

    if (cvg.Items.Count==0) 
        return "Test Fails"; 
    else 
        return FindOldestObjectInCollection(cvg.Items); 
}

上面的 CollectionViewGroup 总是空的——我被引导相信它应该包含组中的所有项目。 (创建为 PagedCollectionView 的组)。 有什么想法我哪里出错了吗?

【问题讨论】:

    标签: c# silverlight datagrid


    【解决方案1】:

    我必须在应用程序中实现类似的功能。您确定在运行转换器时 CollectionViewGroup.Items 已填充吗?它实现了INotifyCollectionChanged,因此可以在组创建后更改。

    因此,您应该使用另一个实现,并直接绑定到 Items 属性,就像我自己做的那样:

    <ControlTemplate>
      <StackPanel>
          <TextBlock x:Name="PropertyNameElement" Margin="4,0,0,0" Visibility="{TemplateBinding PropertyNameVisibility}"/>
          <TextBlock x:Name="ItemCountElement" Margin="4,0,0,0" Visibility="{TemplateBinding ItemCountVisibility}"/>
          <local:TendencyUserControl ItemsSource="{Binding Items}" Margin="4,0,0,0" VerticalAlignment="Center"/>
      </StackPanel>
    </ControlTemplate>
    

    上面的 XAML 适用于我的情况。

    【讨论】:

    • 有什么想法可以每次都传递“当前组”中的项目而不是整个集合吗?即使将名称传递给控件,​​我也无法在当前组中找到纯粹的项目。
    • 组标题上下文中可用的 items 属性已经只包含组项。
    • 谢谢。我假设 [Items] 是我用来填充数据网格的 ItemsSource。没有意识到它本身就是一个属性。
    猜你喜欢
    • 2012-05-24
    • 2010-10-05
    • 2020-08-30
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多