1

I am using an ItemsControl which has a list of Objects... inside the ItemContainerTemplate I have another DataTemplate. Inside of that DataTemplate I can't bind to the ItemsControl.ItemSource anymore.

Since I am not very good with bindings via Find Ancestor and RelativeSource etc. I tried everything without even knowing how to properly use them...

<ItemsControl ItemsSource="{Binding NoteList}">
  <ItemsControl.ItemTemplate>
    <ItemContainerTemplate>
      <GroupBox Header="{Binding Title}"
                Name="MyNoteList"
                Style="{DynamicResource MaterialDesignCardGroupBox}"
                Margin="16">
        <GroupBox.HeaderTemplate>
          <DataTemplate>
            <DockPanel>
              <TextBlock Margin="8,0,0,0"
                         VerticalAlignment="Center"
                         Style="{StaticResource MaterialDesignSubheadingTextBlock}"
                         Text="{Binding}" />
              <Button Padding="5"
                      Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"
                      CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.NoteId}"
                      HorizontalAlignment="Right"
                      Background="Transparent"
                      BorderBrush="Transparent">
                <materialDesign:PackIcon Kind="Delete"
                                         Height="25"
                                         Width="25"
                                         VerticalAlignment="Center" />
              </Button>
            </DockPanel>
          </DataTemplate>
        </GroupBox.HeaderTemplate>
      </GroupBox>
    </ItemContainerTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

I like to mention, that I want to bind to an object from NoteList with the name NoteId inside the CommandParameter. But I keep getting "null" from it. The binding inside my ViewModel is definitely correct. So i only need to know, how to access the NoteId Property from my NoteList

BionicCode
  • 1
  • 4
  • 28
  • 44

1 Answers1

1

I actually got the Problem solved myself! xD ! I did only have to bind to an Element inside my ItemContainerTemplate, in my case to the GroupBox (seen in the code snippet above).

CommandParameter="{Binding DataContext.NoteId, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type GroupBox}}}"
BionicCode
  • 1
  • 4
  • 28
  • 44
  • 1
    Thats true if you are accessing an `Item` inside an `DataTemplate`, in my case, iam inside an `DataTemplate` which is in another `ItemContainerTemplate`, i want to acces the Parent of the `DataTemplate` iam already inside :) – TechPiranja Jul 13 '19 at 11:54