1
votes

how do i binding itemcount to my datacontext to count all the items in each of my groups and then show it in the header

Mærke is the PropertyGroupDescription where my items are binding to

view model

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"));

data context

<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>

it worked before with this

<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 Error: 4 : Cannot find source for binding with reference
'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''.
BindingExpression:Path=ItemCount; DataItem=null;
target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

1
Without knowing what your DataContext is, that is kind of hard to guess. Maybe if you would post your ViewModel we can help.Manfred Radlwimmer
i had updatet it now with thatomini data
hard to guess, but shooting blind: try DataContext = this;or hor
Is ItemCount the only binding not working ? Is the other Billed, Maerke binding working ? To more easily debug the binding, you can check the debugger output window. There is also a lot of details here about how to find the root cause of the binding issue: stackoverflow.com/questions/4026543/…Vincent
missing? the other things that are more in my code other than that i had alredy postet is the design and SQL connection which are working fine and dont have anything to do with this so i dont see a need for that as is just the binding that are problems withomini data

1 Answers

1
votes

this here should work

<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>