简体   繁体   English

如何正确绑定到数据模板中的项目源

[英]How to correctly bind to an Itemsource inside an datatemplate

I am using an ItemsControl which has a list of Objects... inside the ItemContainerTemplate I have another DataTemplate . 我正在使用具有对象列表的ItemsControl ...在ItemContainerTemplate内部,我还有另一个DataTemplate Inside of that DataTemplate I can't bind to the ItemsControl.ItemSource anymore. 在该DataTemplate内部,我无法再绑定到ItemsControl.ItemSource

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... 由于我对通过Find AncestorRelativeSource等进行的绑定不是很好。我尝试了所有操作,甚至都不知道如何正确使用它们...

<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 . 我想提一提,我希望绑定到从对象NoteList名为NoteId里面CommandParameter But I keep getting "null" from it. 但是我一直从中得到“空”。 The binding inside my ViewModel is definitely correct. 我的ViewModel内部的绑定肯定是正确的。 So i only need to know, how to access the NoteId Property from my NoteList 所以我只需要知道如何从我的NoteList访问NoteId属性

I actually got the Problem solved myself! 我实际上已经解决了问题! xD ! 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). 我只需要绑定到ItemContainerTemplate内部的Element,就我而言,就是绑定到GroupBox (在上面的代码段中看到)。

CommandParameter="{Binding DataContext.NoteId, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type GroupBox}}}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM