如何在单击事件中获取 DataTemplate 的父级(在本例中为 Expander 控件)?
How can I get the parent (Expander control in this case) of my DataTemplate in a Click Event?
所以我将这个 XAML 作为我的用户控件。我需要为每个 属性 使用一个扩展器。在 Expander 的 Header 属性 中,我已经绑定到 DataTemplate 以使用图像按钮。我需要的是在单击图像按钮时编辑扩展器内的字段(启用它们)。但是因为它在 DataTemplate 内部,所以我看不到要求它是 "major parent" (Expander) 然后要求父级 (Expander) 启用字段的方法。
这是我的代码XAML(变量是西班牙语)
<UserControl x:Class="Guardian_GUI.Vistas.Recursos.AcordionSupertipos"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="700" d:DesignWidth="1500">
<UserControl.Resources>
<DataTemplate x:Key="DataTemplate1">
<Grid Name="conten" >
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding}" Margin="15 8 15 0" ></TextBlock>
<Button Click="EditarValores" Background="Transparent" Template="{DynamicResource imagenBoton}" Grid.Column="1"/>
</Grid>
</DataTemplate>
<Style TargetType="DockPanel" x:Key="CeldaPropiedades">
<Setter Property="Background" Value="#d08e38"/>
<Setter Property="Margin" Value="5" />
<Setter Property="MinHeight" Value="35" />
</Style>
<Style TargetType="DockPanel" x:Key="CeldaValores">
<Setter Property="Background" Value="#d08e38"/>
<Setter Property="Margin" Value="5" />
<Setter Property="Height" Value="35" />
</Style>
</UserControl.Resources>
<Grid>
<ScrollViewer Name="scroll" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Grid.Row="2" Margin="0 0 0 40">
<StackPanel >
<ItemsControl Name="lista" BorderThickness="0"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding ListaSuperTiposEnumerados}" Margin="0" Padding="0" Background="Transparent">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander Header="{Binding Nombre}" BorderThickness="1" Width="1450" HeaderTemplate="{DynamicResource DataTemplate1}" BorderBrush="#8b8b8b" FontFamily="Myriad Pro" FontStyle="Italic"
FontSize="18" Foreground="#f2f2f2" Background="#333333" VerticalAlignment="Top" Padding="10" >
<ScrollViewer Height="400" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="-10,-10" >
<StackPanel>
<StackPanel Margin="0 20 0 0" >
<DockPanel Grid.Row="0" Grid.ColumnSpan="1" Background="#89673c" Margin="5" MinHeight="35">
<TextBlock Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="24" Text="Valores" Grid.Column="0" Grid.Row="0" Margin="0,0,107,0"/>
<CheckBox Content="Editar valores" Name="editadorValores" Margin="0 8 0 0" Foreground="#f2f2f2"/>
</DockPanel>
<Grid Margin="10,0,0,0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140"/>
<ColumnDefinition Width="440"/>
<ColumnDefinition Width="400"/>
<ColumnDefinition Width="160"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<DockPanel Grid.Column="0" Grid.Row="1" Style="{StaticResource CeldaValores}" >
<TextBlock Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="20" Text="Id" Grid.Column="0" Grid.Row="1" Margin="0,0,107,0"/>
</DockPanel>
<DockPanel Grid.Column="1" Grid.Row="1" Style="{StaticResource CeldaValores}" >
<TextBlock Name="valores" Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="20"
Text="Nombre" Grid.Column="1" Grid.Row="1" IsEnabled="{Binding ElementName=algod, Path=IsChecked}" Margin="0,0,107,0"/>
</DockPanel>
<DockPanel Grid.Column="2" Grid.Row="1" Style="{StaticResource CeldaValores}" >
<TextBlock Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="20" Text="Descripción" Grid.Column="2" Grid.Row="1" Margin="0,0,107,0"/>
</DockPanel>
<DockPanel Grid.Column="3" Grid.Row="1" Style="{StaticResource CeldaValores}" >
<TextBlock Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="20" Text="Valor" Grid.Column="3" Grid.Row="1" Margin="0,0,0,0"/>
</DockPanel>
</Grid>
<ItemsControl Name="lista_2" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding ValoresSuperTipo}" Margin="0 0 0 15" Padding="0" Background="Transparent"
>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="10,5,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140"/>
<ColumnDefinition Width="440"/>
<ColumnDefinition Width="400"/>
<ColumnDefinition Width="160"/>
</Grid.ColumnDefinitions>
<DockPanel Grid.Column="0" Style="{StaticResource CeldaValores}" >
<TextBox ToolTip="{Binding Id}" Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="16"
Text="{Binding Id}" Grid.Column="0" Margin="0,0,0,0" Width="100" IsEnabled="{Binding IsChecked, ElementName=editadorValores}"/>
</DockPanel>
<DockPanel Grid.Column="1" Style="{StaticResource CeldaValores}" >
<TextBox ToolTip="{Binding Nombre}" Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="16"
Text="{Binding Nombre}" Grid.Column="0" Margin="0,0,0,0" Width="400" IsEnabled="{Binding IsChecked, ElementName=editadorValores}"/>
</DockPanel>
<DockPanel Grid.Column="2" Style="{StaticResource CeldaValores}" >
<TextBox ToolTip="{Binding Descripcion}" Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="16" Width="360"
Text="{Binding Descripcion}" Grid.Column="0" Margin="0,0,0,0" IsEnabled="{Binding IsChecked, ElementName=editadorValores}"/>
</DockPanel>
<DockPanel Grid.Column="3" Style="{StaticResource CeldaValores}">
<TextBox ToolTip="{Binding valor}" Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="16" Width="120"
Text="{Binding valor}" Grid.Column="0" Margin="0,0,0,0" IsEnabled="{Binding IsChecked, ElementName=editadorValores}"/>
</DockPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</StackPanel>
</ScrollViewer>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
<Button Content="Guardar" Style="{DynamicResource Boton_1}" HorizontalAlignment="Right" Width="120" VerticalAlignment="Bottom" Grid.Row="3" Click="BotonPersistir"/>
<Button Content="Volver" Style="{DynamicResource Boton_1}" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="120" Margin="0,0,120,0" Click="BotonVolver" />
</Grid>
</UserControl>
当事件运行时,我要求 Parent 和 TemplatedParent,但它们都不是我需要的 Expander。
private void EditarValores(object sender, RoutedEventArgs e)
{
Button boton = sender as Button;
var Parent = boton.Parent; //It returns a Grid
var TemplatedParent = boton.TemplatedParent; //It returns a ContentPresenter
}
感谢您的帮助
来自 : this blog
您可以 运行 在您的按钮上使用此方法来获取扩展器
如:
Expander expander = FindMyParentHelper<Expander>.FindAncestor(boton);
public static class FindMyParentHelper<T> where T : DependencyObject
{
public static T FindAncestor(DependencyObject dependencyObject)
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindAncestor(parent);
}
}
所以我将这个 XAML 作为我的用户控件。我需要为每个 属性 使用一个扩展器。在 Expander 的 Header 属性 中,我已经绑定到 DataTemplate 以使用图像按钮。我需要的是在单击图像按钮时编辑扩展器内的字段(启用它们)。但是因为它在 DataTemplate 内部,所以我看不到要求它是 "major parent" (Expander) 然后要求父级 (Expander) 启用字段的方法。
这是我的代码XAML(变量是西班牙语)
<UserControl x:Class="Guardian_GUI.Vistas.Recursos.AcordionSupertipos"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="700" d:DesignWidth="1500">
<UserControl.Resources>
<DataTemplate x:Key="DataTemplate1">
<Grid Name="conten" >
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding}" Margin="15 8 15 0" ></TextBlock>
<Button Click="EditarValores" Background="Transparent" Template="{DynamicResource imagenBoton}" Grid.Column="1"/>
</Grid>
</DataTemplate>
<Style TargetType="DockPanel" x:Key="CeldaPropiedades">
<Setter Property="Background" Value="#d08e38"/>
<Setter Property="Margin" Value="5" />
<Setter Property="MinHeight" Value="35" />
</Style>
<Style TargetType="DockPanel" x:Key="CeldaValores">
<Setter Property="Background" Value="#d08e38"/>
<Setter Property="Margin" Value="5" />
<Setter Property="Height" Value="35" />
</Style>
</UserControl.Resources>
<Grid>
<ScrollViewer Name="scroll" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Grid.Row="2" Margin="0 0 0 40">
<StackPanel >
<ItemsControl Name="lista" BorderThickness="0"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding ListaSuperTiposEnumerados}" Margin="0" Padding="0" Background="Transparent">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander Header="{Binding Nombre}" BorderThickness="1" Width="1450" HeaderTemplate="{DynamicResource DataTemplate1}" BorderBrush="#8b8b8b" FontFamily="Myriad Pro" FontStyle="Italic"
FontSize="18" Foreground="#f2f2f2" Background="#333333" VerticalAlignment="Top" Padding="10" >
<ScrollViewer Height="400" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="-10,-10" >
<StackPanel>
<StackPanel Margin="0 20 0 0" >
<DockPanel Grid.Row="0" Grid.ColumnSpan="1" Background="#89673c" Margin="5" MinHeight="35">
<TextBlock Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="24" Text="Valores" Grid.Column="0" Grid.Row="0" Margin="0,0,107,0"/>
<CheckBox Content="Editar valores" Name="editadorValores" Margin="0 8 0 0" Foreground="#f2f2f2"/>
</DockPanel>
<Grid Margin="10,0,0,0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140"/>
<ColumnDefinition Width="440"/>
<ColumnDefinition Width="400"/>
<ColumnDefinition Width="160"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<DockPanel Grid.Column="0" Grid.Row="1" Style="{StaticResource CeldaValores}" >
<TextBlock Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="20" Text="Id" Grid.Column="0" Grid.Row="1" Margin="0,0,107,0"/>
</DockPanel>
<DockPanel Grid.Column="1" Grid.Row="1" Style="{StaticResource CeldaValores}" >
<TextBlock Name="valores" Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="20"
Text="Nombre" Grid.Column="1" Grid.Row="1" IsEnabled="{Binding ElementName=algod, Path=IsChecked}" Margin="0,0,107,0"/>
</DockPanel>
<DockPanel Grid.Column="2" Grid.Row="1" Style="{StaticResource CeldaValores}" >
<TextBlock Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="20" Text="Descripción" Grid.Column="2" Grid.Row="1" Margin="0,0,107,0"/>
</DockPanel>
<DockPanel Grid.Column="3" Grid.Row="1" Style="{StaticResource CeldaValores}" >
<TextBlock Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="20" Text="Valor" Grid.Column="3" Grid.Row="1" Margin="0,0,0,0"/>
</DockPanel>
</Grid>
<ItemsControl Name="lista_2" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding ValoresSuperTipo}" Margin="0 0 0 15" Padding="0" Background="Transparent"
>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="10,5,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140"/>
<ColumnDefinition Width="440"/>
<ColumnDefinition Width="400"/>
<ColumnDefinition Width="160"/>
</Grid.ColumnDefinitions>
<DockPanel Grid.Column="0" Style="{StaticResource CeldaValores}" >
<TextBox ToolTip="{Binding Id}" Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="16"
Text="{Binding Id}" Grid.Column="0" Margin="0,0,0,0" Width="100" IsEnabled="{Binding IsChecked, ElementName=editadorValores}"/>
</DockPanel>
<DockPanel Grid.Column="1" Style="{StaticResource CeldaValores}" >
<TextBox ToolTip="{Binding Nombre}" Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="16"
Text="{Binding Nombre}" Grid.Column="0" Margin="0,0,0,0" Width="400" IsEnabled="{Binding IsChecked, ElementName=editadorValores}"/>
</DockPanel>
<DockPanel Grid.Column="2" Style="{StaticResource CeldaValores}" >
<TextBox ToolTip="{Binding Descripcion}" Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="16" Width="360"
Text="{Binding Descripcion}" Grid.Column="0" Margin="0,0,0,0" IsEnabled="{Binding IsChecked, ElementName=editadorValores}"/>
</DockPanel>
<DockPanel Grid.Column="3" Style="{StaticResource CeldaValores}">
<TextBox ToolTip="{Binding valor}" Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="16" Width="120"
Text="{Binding valor}" Grid.Column="0" Margin="0,0,0,0" IsEnabled="{Binding IsChecked, ElementName=editadorValores}"/>
</DockPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</StackPanel>
</ScrollViewer>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
<Button Content="Guardar" Style="{DynamicResource Boton_1}" HorizontalAlignment="Right" Width="120" VerticalAlignment="Bottom" Grid.Row="3" Click="BotonPersistir"/>
<Button Content="Volver" Style="{DynamicResource Boton_1}" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="120" Margin="0,0,120,0" Click="BotonVolver" />
</Grid>
</UserControl>
当事件运行时,我要求 Parent 和 TemplatedParent,但它们都不是我需要的 Expander。
private void EditarValores(object sender, RoutedEventArgs e)
{
Button boton = sender as Button;
var Parent = boton.Parent; //It returns a Grid
var TemplatedParent = boton.TemplatedParent; //It returns a ContentPresenter
}
感谢您的帮助
来自 : this blog
您可以 运行 在您的按钮上使用此方法来获取扩展器
如:
Expander expander = FindMyParentHelper<Expander>.FindAncestor(boton);
public static class FindMyParentHelper<T> where T : DependencyObject
{
public static T FindAncestor(DependencyObject dependencyObject)
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindAncestor(parent);
}
}