tags:

views:

580

answers:

1

I'm trying to implement a datagrid that works like a folder tree (i.e. each row represents a folder and the details view is another datagrid showing files in the folder). I've tried to trim down my code for simplicity, so there could be mistakes, but here is my basic XAML layout:

<my:DataGrid Name="dataGrid1" AutoGenerateColumns="False" ItemsSource="{Binding}">
    <my:DataGrid.RowDetailsTemplate>
     <DataTemplate>
      <my:DataGrid ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:DataGrid}}, Path=SelectedItem.Files}" AutoGenerateColumns="False" HeadersVisibility="None">
       <my:DataGrid.Columns>
        <my:DataGridTextColumn Binding="{Binding Path=FileName}" />
        <my:DataGridTextColumn Binding="{Binding Path=FSize}" />
       </my:DataGrid.Columns>
      </my:DataGrid>
     </DataTemplate>
    </my:DataGrid.RowDetailsTemplate>
    <my:DataGrid.Columns>
     <my:DataGridTemplateColumn CanUserResize="False" CanUserSort="False" Width="16">
      <my:DataGridTemplateColumn.CellTemplate>
       <DataTemplate>
        <Button Click="Button_Click_1">
         <Image Source="resources/+.ico" />
        </Button>
       </DataTemplate>
      </my:DataGridTemplateColumn.CellTemplate>
     </my:DataGridTemplateColumn>
     <my:DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" />
    </my:DataGrid.Columns>
</my:DataGrid>

I'm trying to have the button expand the details row, but I'm not sure how to reference the row:

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        // how do I reference a row here?
        DataGridRow row = ?
        // so I can do this:
        if (row.DetailsVisibility == Visibility.Collapsed)
            row.DetailsVisibility = Visibility.Visible;
        else
            row.DetailsVisibility = Visibility.Collapsed;
    }

Hopefully the question is clear... Thanks.

+2  A: 

private void Button_Click_1(object sender, RoutedEventArgs e) { DependencyObject obj = (DependencyObject)e.OriginalSource; while (!(obj is DataGridRow) && obj != null) obj = VisualTreeHelper.GetParent(obj);

        if(obj is DataGridRow)
            (obj as DataGridRow).DetailsVisibility = Visibility.Visible;
    }

...et voilà