将itemcount绑定到datacontext

时间:2016-05-11 07:36:25

标签: c# xaml

如何将itemcount绑定到我的datacontext来计算每个组中的所有项目,然后在标题中显示

Mærke是我的项目绑定到

PropertyGroupDescription

查看模型

string Data = 
    @"Select ps.Mærket AS Mærke, P.DataID, P.Billed, P.Model, 
          P.Årgang, P.[Motor Type], P.Krydsmål, P.Centerhul, 
          P.ET,P.Bolter, P.Dæk, P.Fælge ,PS.Krydsmålene
      from Data.Hjuldata P  
      inner join Data.Mærke PS on P.MærkeID = PS.MærkeID 
      ORDER BY ps.Mærket";

ICollectionView dataView = CollectionViewSource.GetDefaultView(hjuldata.ItemsSource);
dataView.GroupDescriptions.Add(new PropertyGroupDescription("Mærke"));

数据上下文

<Expander.Header>
    <StackPanel Orientation="Horizontal" DataContext="{Binding Items}">
        <Image Source="{Binding Billed}" Width="20" Height="20" Stretch="Fill" VerticalAlignment="Center"  Margin="0,0,15,0"/>
        <TextBlock Text="{Binding Mærke}" FontWeight="Bold" Foreground="#FFEAEAEA" FontSize="22" VerticalAlignment="Bottom" />
        <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" VerticalAlignment="Bottom"/>
        <TextBlock Text="{Binding Krydsmålene}"  FontWeight="Bold" Foreground="#FFFBFB00" FontSize="22" VerticalAlignment="Bottom" Margin="0,0,150,0" TextAlignment="Center" />
    </StackPanel>
</Expander.Header>

以前用这个

<Expander.Header>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" VerticalAlignment="Bottom"/>
        <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="#FFEAEAEA" FontSize="22 "VerticalAlignment="Bottom"/>                                          
    </StackPanel>
</Expander.Header>
  

System.Windows.Data错误:4:无法找到与引用的绑定源   'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.UserControl',AncestorLevel ='1''。
  BindingExpression:路径= ItemCount中;的DataItem = NULL;
  target元素是'TextBlock'(Name =''); target属性是'Text'(类型'String')

1 个答案:

答案 0 :(得分:1)

这里应该有效

<TextBlock FontSize="22" Foreground="Green" FontWeight="Bold" FontStyle="Italic" VerticalAlignment="Bottom">
<Run Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListView}}, Path=Items.Count, Mode=OneWay}"/>
</TextBlock>