none
How to access Binding properties other than 'Name' in a ListView group? RRS feed

  • Question

  • I have a ListView bound to a CollectionViewSource backed by a list of objects with multiple properities. Binding to the 'Name' property on each of my objects works fine, but I can't seem to figure out how to bind to any other properties on my object. When I try, I get this runtime error in my output window:

    Code Block
    System.Windows.Data Error: 35 : BindingExpression path error: 'CategoryId' property not found on 'object' ''CollectionViewGroupInternal' (HashCode=53740117)'. BindingExpression:Path=CategoryName; DataItem='CollectionViewGroupInternal' (HashCode=53740117); target element is 'Button' (Name=''); target property is 'Content' (type 'Object')

     

     


    It appears that the system is resolving 'CategoryId' against the CollectionViewGroupInternal, rather than my object in the collection.

    How do I write my binding so that my Path property applies to the actual objects in my CollectionViewSource rather than the CollectionViewGroupInternal object?
    Thanks,
    --Jeremy

    === Edited by Jeremy Chaney @ 01 Nov 2007 10:34 PM UTC===
    As I continued to research this problem, I've found new techniques that lead me to new errors, but it seems like I might be getting closer. If I change my Content binding to "Content="{Binding Path=/CategoryId}"" then the error is "'CategoryId' property not found on 'current item of collection' ''CollectionViewGroupInternal'". If the 'current item of collection' is an object from my list, then 'Category' id should be found....
    Thursday, November 1, 2007 6:07 PM

Answers

  • Hi Jeremy,

     

        GroupItem's DataContext will point to the CollectionViewGroup object rather than your Procedure data object. If the CategoryId within a particular group is the same, then you can change the Binding syntax on CommandParameter to something like the following:

     

    Code Block

    <Button Template="{DynamicResource GlassButton}" Content="Delete"

            Command="controls:CategoryBrowser.DeleteCategoryCommand"

            CommandParameter="{Binding Path=Items[0].CategoryId}"/>

     

     

         Hope this helps

    Wednesday, November 7, 2007 3:36 AM

All replies

  • Hi Jeremy,

     

       Could you please show us how your data collection looks like and how do you bind those data to UI in XAML?

     

       Thanks

    Monday, November 5, 2007 5:24 AM
  • My data collection is an  ObservableCollection<Procedure>, where Procedure contains a method called CategoryName that is used to group the items, and another method CategoryID that I want to bind to a button (the content="Delete" button). Here is my XAML


    Code Block

    <ListView.GroupStyle>
              <GroupStyle>
                <GroupStyle.ContainerStyle>
                  <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Margin" Value="0,0,0,5"/>
                    <Setter Property="Template">
                      <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                          <Expander IsExpanded="True" BorderBrush="#FFA4B97F"
                                  BorderThickness="0,0,0,1" HeaderTemplate="{StaticResource HeaderTemplate}">

                            <Expander.Header>
                              <Grid VerticalAlignment="Top" Height="23">
                                <Grid.ColumnDefinitions>
                                  <ColumnDefinition Width="Auto"/>
                                  <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <TextBox Style="{StaticResource CategoryBrowserGroupHeaderTextBoxStyle}" Text="{Binding ElementName=CategoryName, Path=Text}" FontWeight="Bold" FontSize="15" Grid.Column="0" IsReadOnly="True" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="#FFFFFFFF"/>
                                <TextBlock x:Name="CategoryName" Visibility="Collapsed"
                                           Text="{Binding Path=Name}"/>
                                <StackPanel Margin="0,0,0,0" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right">
                                  <Button Template="{DynamicResource GlassButton}" Content="Add" Click="OnNewCategory"/>
                                  <Button Template="{DynamicResource GlassButton}" Content="Delete" Command="controls:CategoryBrowser.DeleteCategoryCommand" CommandParameter="{Binding Path=CategoryId}"/>

                                </StackPanel>
                              </Grid>
                            </Expander.Header>
                            <Expander.Content>
                              <ItemsPresenter />
                            </Expander.Content>
                          </Expander>

                        </ControlTemplate>
                      </Setter.Value>
                    </Setter>
                  </Style>
                </GroupStyle.ContainerStyle>
              </GroupStyle>
            </ListView.GroupStyle>


    Tuesday, November 6, 2007 11:04 PM
  • Hi Jeremy,

     

        GroupItem's DataContext will point to the CollectionViewGroup object rather than your Procedure data object. If the CategoryId within a particular group is the same, then you can change the Binding syntax on CommandParameter to something like the following:

     

    Code Block

    <Button Template="{DynamicResource GlassButton}" Content="Delete"

            Command="controls:CategoryBrowser.DeleteCategoryCommand"

            CommandParameter="{Binding Path=Items[0].CategoryId}"/>

     

     

         Hope this helps

    Wednesday, November 7, 2007 3:36 AM
  • In this case the category ID is the same, but what if it wasn't. Is there some way to know what index each item is at?

    Wednesday, November 7, 2007 3:43 AM
  •    This might be irrelevant to your question. Since you group the items based on the CategoryName, then those items within a specified group should have the same CategoryId, otherwise your binding on the "Delete" button doesn't take sense at all.

     

       As to your question, as far as I know there is no way from XAML to know which index each item is at, but you can grab the CollectionViewGroup instance yourself and employ some logic in codebehind to make such a judgement yourself.

     

       Thanks

     

    Wednesday, November 7, 2007 6:58 AM