简体   繁体   English

WPF DataTemplate-父对象的访问属性

[英]WPF DataTemplate - Access Property of Parent object

I am customizing the presentation using the HeaderTemplate of a UI item. 我正在使用UI项的HeaderTemplate自定义演示文稿。 I would like to access a property of the parent item in the DataTemplate: 我想在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. 基本上,我想在DataTemplate中显示Tag属性。

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}"

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

相关问题 WPF ListBox使用DataTemplate显示对象属性 - WPF ListBox showing object property using DataTemplate 在 WPF 应用程序中使用 Styles 或 DataTemplate 更改控件子 object 的属性 - Change property of controls child object by using Styles or DataTemplate in WPF application Xamarin Forms - 来自父对象的 DataTemplate 中嵌套属性的 DataTemplate - Xamarin Forms - DataTemplate from Nested Property within DataTemplate from Parent Object 如何从DataTemplate访问ListView的ItemsSource中每个对象的属性路径 - How to access a property path of each object in ItemsSource of a ListView from a DataTemplate 获取由DataTemplate实例化的对象的父对象 - Get parent of object instantiated by DataTemplate XAML - 如何从 ControlTemplate 中的子控件访问父控件的 DataTemplate 属性? - XAML - How do I access a parent control's DataTemplate property from a child in a ControlTemplate? WPF DataTemplate绑定取决于属性的类型 - WPF DataTemplate Binding depending on the type of a property WPF:修改DataTemplate的ControlTemplate属性? - WPF: Modifying a ControlTemplate property from a DataTemplate? WPF将ItemContainerStyle中的属性绑定到ItemTemplate的DataTemplate中的TextBlock - Wpf bind property in ItemContainerStyle to TextBlock in DataTemplate of ItemTemplate WPF:使用绑定属性自定义数据模板 - WPF: Customize a datatemplate using a binding property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM