在WPF DataGrid中显示层次化的父子数据 [英] Displaying hierarchal parent child data in WPF DataGrid

查看:168
本文介绍了在WPF DataGrid中显示层次化的父子数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道有没有身体能给我一些关于这个问题的指针?



我需要能够在WPF数据网格中显示父/子行,父级会显示加/减控件,当单击时显示相关的子记录。



为了做到这一点,我基本上在父网格RowDetailsTemplate和一个样式触发器中放置一个第二个datagrid来翻转每一行的detailsvisibility属性。例如

 < Grid> 
< DataGrid AutoGenerateColumns =FalseIsReadOnly =TrueItemsSource =sourceMargin =10,10,14,14Name =dataGrid1RowDetailsVisibilityMode =CollapsedSelectionMode =Single>
< DataGrid.Columns>
< DataGridTextColumn Binding ={Binding FullName}Header =Name/>
< DataGridTextColumn Binding ={Binding Details}Header =Details/>
< /DataGrid.Columns>
< DataGrid.RowDetailsTemplate>
< DataTemplate>
< DataGrid HeadersVisibility =NoneAutoGenerateColumns =FalseIsReadOnly =TrueItemsSource ={Binding Path = Attachments}>
< DataGrid.Columns>
< DataGridTextColumn Binding ={Binding Document}Header =Document/>
< DataGridTextColumn Binding ={Binding Created}Header =创建日期/>
< /DataGrid.Columns>
< / DataGrid>
< / DataTemplate>
< /DataGrid.RowDetailsTemplate>
< DataGrid.RowHeaderTemplate>
< DataTemplate>
< ToggleButton IsChecked ={Binding Converter = {StaticResource visibleConverter},RelativeSource = {RelativeSource AncestorType = {x:Type DataGridRow}},Path = DetailsVisibility}>
< Image>
< Image.Style>
< Style TargetType =Image>
< Style.Triggers>
< DataTrigger Binding ={Binding RelativeSource = {RelativeSource AncestorType = ToggleButton},Path = IsChecked}Value =false>
< Setter Property =Image.SourceValue =Images / plus-8.png/>
< / DataTrigger>
< DataTrigger Binding ={Binding RelativeSource = {RelativeSource AncestorType = ToggleButton},Path = IsChecked}Value =true>
< Setter Property =Image.SourceValue =Images / minus-8.png/>
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /Image.Style>
< / Image>
< / ToggleButton>
< / DataTemplate>
< /DataGrid.RowHeaderTemplate>
< / DataGrid>
< / Grid>

问题1是当我使用键盘上下浏览父网格时,它会跳过子网格,跳过RowDetailsTemplate区域。



使用多个网格的另一个副作用是,如果我使用鼠标选择行,我可以获得多个选定行效果,即一个的父母被选中,而其他父母以前在内部网格中选择的其他孩子记录看起来仍然被突出显示。



任何人都可以给我任何指针关于如何解决这些问题?



这里是在上面的XAML中引用的visibilityConverter的代码;



许多感谢....

  public class VisibleConverter:IValueConverter 
{
public object Convert值,类型targetType,对象参数,CultureInfo文化)
{
bool visibleValue = false;
if(value!= null&& value is Visibility)
{
visibleValue =!((Visibility)value == Visibility.Collapsed)
}
return visibleValue;
}

public object ConvertBack(object value,Type targetType,object parameter,CultureInfo culture)
{
可见性visibleValue = Visibility.Collapsed;
if(value is bool)
{
if((bool)value == true)
{
visibleValue = Visibility.Visible;
}
}
return visibleValue;
}
}


解决方案

我有同样的要求。我以下列方式实施了这一条件。当扩展器打开时,您可以使用以下代码。

  private void Expander_Expanded(object sender,RoutedEventArgs e)
{
foreach(viewModel.Results中的var项目)
{
if(item.SubResults.Count> 0)
{
item.SubResults.SelectedItem =空值;
}
}
}

其中结果是项目来源对于我的外部网格,Subresults是我的内部网格的数据源。当打开扩展器时,此代码有助于重置所选项。


wonder if any body can give me some pointers on this problem?

I need to be able to display parent / child rows in a WPF datagrid, the parent are displayed with a plus / minus control which when clicked shows the related children records.

In order to do this I basically place a second datagrid inside the parent grids RowDetailsTemplate and a style trigger to flip the detailsvisibility property on each row. e.g.

<Grid>
    <DataGrid AutoGenerateColumns="False" IsReadOnly="True" ItemsSource="source" Margin="10,10,14,14" Name="dataGrid1" RowDetailsVisibilityMode="Collapsed" SelectionMode="Single">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding FullName}" Header="Name" />
            <DataGridTextColumn Binding="{Binding Details}" Header="Details" />
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <DataGrid HeadersVisibility="None" AutoGenerateColumns="False" IsReadOnly="True" ItemsSource="{Binding Path=Attachments}">
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding Document}" Header="Document" />
                        <DataGridTextColumn Binding="{Binding Created}" Header="Date Created" />
                    </DataGrid.Columns>
                </DataGrid>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
        <DataGrid.RowHeaderTemplate>
            <DataTemplate>
                <ToggleButton IsChecked="{Binding Converter={StaticResource visibleConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path=DetailsVisibility}">
                    <Image>
                        <Image.Style>
                            <Style TargetType="Image">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton}, Path=IsChecked}" Value="false">
                                        <Setter Property="Image.Source" Value="Images/plus-8.png" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton}, Path=IsChecked}" Value="true">
                                        <Setter Property="Image.Source" Value="Images/minus-8.png" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                </ToggleButton>
            </DataTemplate>
        </DataGrid.RowHeaderTemplate>
    </DataGrid>
</Grid>

Problem 1 is when i use the keyboard to navigate up and down the parent grid it skips the child grid, skipping over the RowDetailsTemplate area.

Another side effect of using multiple grids is if I use the mouse to select rows I can get multiple 'selected' rows effect, ie one of the parents is selected and other child records for other parents that have been previously selected in the inner grids look like they are still highlighted.

Can anybody give me any pointers here as to how to get round these problems?

Here the code for the visibilityConverter referenced in the XAML above ;

Many Thanks....

 public class VisibleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool visibleValue = false;
        if (value != null && value is Visibility)
        {
            visibleValue = !((Visibility)value == Visibility.Collapsed);
        }
        return visibleValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Visibility visibleValue = Visibility.Collapsed;
        if (value is bool)
        {
            if ((bool)value == true)
            {
                visibleValue = Visibility.Visible;
            }
        }
        return visibleValue;
    }
}

解决方案

Even i have the same requirement. I have implemented the condition in following way. When the expander is opened, you can have the following code.

private void Expander_Expanded(object sender, RoutedEventArgs e)
{
   foreach (var item in viewModel.Results)
   {
     if (item.SubResults.Count > 0)
      {
        item.SubResults.SelectedItem = null;
      }
   }
}

Where Results is the items source for my outer grid, and Subresults is the datasource for my inner grid. This code helps to reset the selected item, whenever the expander is opened.

这篇关于在WPF DataGrid中显示层次化的父子数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆