我正在尝试实现一个像文件夹树一样工作的数据网格(即每行代表一个文件夹,详细信息视图是另一个显示文件夹中文件的数据网格)。为了简单起见,我试图减少我的代码,所以可能会出错,但这是我的基本XAML布局:
<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>
我正在尝试让按钮展开详细信息行,但我不确定如何引用该行:
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;
}
希望问题很清楚......谢谢。
答案 0 :(得分:6)
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;
}
...etvoilà