Usuario
DataGrid - Obtener DataGridRow desde Button contenido en un DataTemplate

Pregunta
-
hola
la verdad se me complico, aunque parece en principio facil, pero es engañoso
resulta que tengo un DataGrid y dentro de este un DataTemplate de un CellTemplate, dentro de este un Button (mas puntualmente un ImageButton, pero con un boton simple se presenta el mismo problema)
la idea es presionar un boton y poder obtener el DataGridRow que lo contiene
la grilla seria esta:
<DataGrid ItemsSource="{Binding Path=ServicesList}" IsReadOnly="True" AutoGenerateColumns="False" AllowRowSelection="True" SelectedItem="{Binding Path=SelectedService}"
Width="Auto" CanUserReorderColumns="False" CanUserSortColumns="true" CanUserResizeColumns="True" x:Name="ServicesGrid" CanUserAddRows="False" CanUserResizeRows="False" CanUserDeleteRows="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="{x:Static Member=res:Labels.ServiceType}" MinWidth="80" Width="*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ImageButton Margin="2" Width="20" ToolTip="{x:Static Member=res:Labels.RowDetail}" Height="20" VerticalAlignment="Bottom"
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.ButtonCommand}" CommandParameter="{Binding ID}"
Click="ImageButton_Click">
<Image Stretch="UniformToFill" Source="../../Images/down.png" />
</ImageButton>
<TextBlock Text="{Binding Path=ServiceType}" VerticalAlignment="Bottom"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="{x:Static Member=res:Labels.StartDate}" Binding="{Binding Path=StartOfServiceDate, StringFormat=d}" MinWidth="100" Width="*" />
<DataGridTextColumn Header="{x:Static Member=res:Labels.EndDate}" Binding="{Binding Path=EndDateOfService, StringFormat=d}" MinWidth="100" Width="*" />
<DataGridTextColumn Header="{x:Static Member=res:Labels.Amount}" Binding="{Binding Path=Amount}" MinWidth="130" Width="2*" />
</DataGrid.Columns>
</DataGrid>como veran en el ImageButton tengo un evento Click="ImageButton_Click", donde intente usar
private void ImageButton_Click(object sender, RoutedEventArgs e)
{
ImageButton datagridButton = (ImageButton)sender;
DataGridRow dataGridrow = DataGridRow.GetRowContainingElement(datagridButton);
dataGridrow.DetailsVisibility = Visibility.Visible;
}pero el dataGridrowesta en null, o sea el GetRowContainingElement() no me funciono
tambien intente usar
private void ImageButton_Click(object sender, RoutedEventArgs e)
{
DependencyObject dep = (DependencyObject)e.OriginalSource;
while ((dep != null) && !(dep is DataGridRow))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep != null && dep is DataGridRow)
{
DataGridRow row = (DataGridRow)dep;
if (row.DetailsVisibility == Visibility.Collapsed)
row.DetailsVisibility = Visibility.Visible;
else
row.DetailsVisibility = Visibility.Collapsed;
}
}pero tampoco funciono, ya que el "dep" llega un punto que se vuelve nulo
estuve viendo que esto de buscar el parent hasta llegar al DataGridRow tiene un corte en el ContentPresenter
en el primer ciclo el VisualTreeHelper.GetParent() devuelve el StackPanel, en el segundo devuelve el ContentPresenter, y en el siguiente devuelve null y sale sin poder continuar
mas alla de ContentPresenter no se puede buscar los parent
saben si hay alguna forma recuperar el DataGridRow ?
Estos usando VS2010, estoy usando MVVM, pero para esta accion en particular no me importaria pasarlo de largo porque tengo que trabajar con el RowDetailsTemplate de forma programatica desde el boton
saludos
Leandro Tuttini
Blog
Buenos Aires
Argentina
Todas las respuestas
-
Hola Leandro,
No se si sigues con este problema,.... pero se me ocurre que en vez de usar el VisualTreeHelper puedes probar con el LogicalTreeHelper.
Un abrazo!
MCTS .NET Framework 3.5 Windows Forms Application Development
MCTS .NET Framework 3.5 Windows Presentation Foundation
Visita mi Blog en Geeks.ms
Sigueme en Twitter -
Que tal Leandro,
si continuas con el problema, yo trate practicamente lo mismo tengo un grid muy similar y probe usando
DataGridRow row = DataGridRow.GetRowContainingElement((FrameworkElement)sender);
y funciono correctamente.
Saludos, nos comentas como lo resolviste!