【问题标题】:bind itemcount to datacontext将 itemcount 绑定到 datacontext
【发布时间】:2016-09-06 11:08:09
【问题描述】:

如何将 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''。
绑定表达式:路径=项目计数;数据项=null;
目标元素是'TextBlock'(名称='');目标属性是“文本”(类型“字符串”)

【问题讨论】:

  • 在不知道您的 DataContext 是什么的情况下,这很难猜到。也许如果您发布您的 ViewModel,我们可以提供帮助。
  • 我现在已经更新了它
  • 难以猜测,但盲目射击:试试 DataContext = this;
  • 是 ItemCount 唯一的绑定不起作用吗?另一个 Billed,Maerke 绑定是否有效?要更轻松地调试绑定,您可以检查调试器输出窗口。这里也有很多关于如何找到绑定问题的根本原因的细节:stackoverflow.com/questions/4026543/…
  • 不见了?除了我已经 postet 之外,我的代码中更多的其他东西是设计和 SQL 连接,它们工作正常并且与此没有任何关系,所以我认为没有必要,因为只是绑定问题与

标签: c# xaml


【解决方案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>

【讨论】:

  • 这让我更近了一步,现在它在所有标题而不是分组项目上显示了我在整个列表视图中的所有项目
猜你喜欢
  • 2011-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-12
  • 2016-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多