WPF DataTemplate-父对象的访问属性

时间:2018-09-27 04:00:12

标签: c# xaml data-binding datatemplate

我正在使用UI项的HeaderTemplate自定义演示文稿。我想在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>

基本上,我想在DataTemplate中显示Tag属性。

我尝试过:

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

但似乎没有任何作用。

1 个答案:

答案 0 :(得分:0)

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

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