简体   繁体   中英

WPF DataTemplate - Access Property of Parent object

I am customizing the presentation using the HeaderTemplate of a UI item. I would like to access a property of the parent item in the DataTemplate:

    <dxa:AccordionItem Header="{Binding SelectedComponents.Count}" Tag="Test" HighlightOnHover="False" HighlightOnPress="False" Margin="0,0,13,0">
                <dxa:AccordionItem.HeaderTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="30"/>
                                <RowDefinition Height="1"/>
                            </Grid.RowDefinitions>
                            <TextBlock Text="{Binding RelativeSource=Tag}" VerticalAlignment="Center" />
                            <TextBlock Text="{Binding}" Grid.Column="2" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            <Rectangle Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3" HorizontalAlignment="Stretch"  VerticalAlignment="Center" Fill="DimGray" Height="1"/>
                        </Grid>
                    </DataTemplate>
                </dxa:AccordionItem.HeaderTemplate>
</dxa:AccordionItem>

Basically, I would like to display the Tag property in the DataTemplate.

I have tried:

Text="{Binding RelativeSource=Tag}"
Text="{Binding Path=Tag}"
Text="{Binding ElementName=Tag}"

but nothing seems to work.

我设法使用以下方法获得了必要的绑定:

Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dxa:AccordionItem}}, Path=Tag}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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