wpf中的父子网格 [英] Parent-Child Grid in wpf

查看:97
本文介绍了wpf中的父子网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在设计一个wpf应用程序,现在我的要求是我想放置一个父子网格。父记录应该有一个展开按钮,以便孩子的reords将出现在它....请我是这个亲子网格的新手......任何人都可以通过提供代码帮助我....它也被称为嵌套网格

Hello,
I am designing a wpf application, now my requirement is that I want to place a parent child grid. A parent record should posses a expand button so that the child reords will be present in it.... Please I am new to this Parent-child Grid.... Can any one help out me by providing the code....It is also called as Nested Grid

推荐答案

<DataGrid AutoGenerateColumns="False" MaxHeight="525" Height="auto" RowBackground="White"  HeadersVisibility="Column"  HorizontalAlignment="Left"  Background="#E7F1FA" AlternatingRowBackground="LightGray" Name="grdKitsGroupingParent" VerticalAlignment="Top"  CanUserAddRows="False" Width="1130" Margin="5,194,0,0" Grid.Column="1"  RowDetailsVisibilityMode="VisibleWhenSelected" RowDetailsVisibilityChanged="grdKitsGroupingParent_RowDetailsVisibilityChanged">
                      <DataGrid.Columns>
                          <DataGridTemplateColumn Header="C" >
                              <DataGridTemplateColumn.CellTemplate>
                                  <DataTemplate>
                                      <StackPanel Name="stk">
                                          <Button x:Name="btnCollapse"  ToolTip="Expand" Foreground="Black" Background="Transparent" BorderBrush="Transparent" Tag="{Binding KitGroupingID}" Click="btnCollapse_Click"    >
                                              <Image Source="../images/Plus.png" Cursor="Hand"  Height="22" Width="18"  Name="imgExpand" ></Image>
                                          </Button>
                                      </StackPanel>
                                  </DataTemplate>
                              </DataGridTemplateColumn.CellTemplate>
                          </DataGridTemplateColumn>
                          <DataGridTemplateColumn Header="D">
                              <DataGridTemplateColumn.CellTemplate>
                                  <DataTemplate>
                                      <StackPanel>
                                          <Button x:Name="DeleteButton" ToolTip="Delete" Background="Transparent" BorderBrush="Transparent"    Foreground="Black" Tag="{Binding KitGroupingID}" Click="btnDelete_Click"   >
                                              <Image Source="../images/Deleteicon.png" Cursor="Hand" Height="22" Width="18"  ></Image>
                                          </Button>
                                      </StackPanel>
                                  </DataTemplate>
                              </DataGridTemplateColumn.CellTemplate>
                          </DataGridTemplateColumn>
                          <DataGridTemplateColumn Header="S"  >
                              <DataGridTemplateColumn.CellTemplate>
                                  <DataTemplate>
                                      <StackPanel>
                                          <Button x:Name="btnSaveParent"  ToolTip="Save" Foreground="Black" Background="Transparent"  BorderBrush="Transparent"  Tag="{Binding KitGroupingID}" Click="btnSaveParent_Click"  >
                                              <Image Source="../images/Save.png" Cursor="Hand" Height="22" Width="18"   ></Image>
                                          </Button>
                                      </StackPanel>
                                  </DataTemplate>
                              </DataGridTemplateColumn.CellTemplate>
                          </DataGridTemplateColumn>


                          <DataGridTextColumn CanUserReorder="True"  CanUserResize="True" CanUserSort="True" Width="200" Binding="{Binding KitGroupName}" Header="Group Name"  Foreground="Black">
                          </DataGridTextColumn>

                          <DataGridTextColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="190*" Binding="{Binding Inventory}" Header="Inventory"  Foreground="Black">
                          </DataGridTextColumn>

                      </DataGrid.Columns>
                      <DataGrid.RowDetailsTemplate>
                          <DataTemplate>
                              <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"   >
                                  <DataGrid  Name="NestedDG"   CanUserAddRows="False"  Background=" White" AlternatingRowBackground="LightGray" Width="1060"  HeadersVisibility="Column" AutoGenerateColumns="False" RowDetailsVisibilityMode="VisibleWhenSelected" RowDetailsVisibilityChanged="ParentDG_RowDetailsVisibilityChanged"  >
                                      <DataGrid.Columns>

                                          <DataGridTextColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="700*" Binding="{Binding LineItem}" Header="Line Item"  Foreground="Black">
                                          </DataGridTextColumn>

                                      </DataGrid.Columns>
                                   
                  </DataGrid>


如果你要求DataGrid,我不太了解它。



你的意思是当用户在一个ListView(A)中选择父记录时,所有子记录s显示在另一个ListView(B)中?

如果是这样,你必须创建一个表示父节点的ViewModel,它具有链接到子记录的ChildRecords属性,并让(B)绑定到( A)的ChildRecords。



或者你想在同一个ListView中显示?

如果是这样,除了viewmodel,你必须更改ListView的ItemContainerStyle:

If you asking for DataGrid, I don't know too much about it.

Do you meant when user select a parent record in one ListView(A), all child records is shown in another ListView(B)?
If so, you have to create a ViewModel that represent the parents, which have ChildRecords properties that link to child records, and let (B) bind to (A)'s ChildRecords.

Or you want to show in the same ListView?
If so, in addition to the viewmodel, you have to change ListView's ItemContainerStyle:
<Page

  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  
  <Grid>  
    
    <ListView>
      <ListView.ItemContainerStyle>
      
        <Style TargetType="ListBoxItem">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="ListBoxItem">
                <StackPanel>
                  <ContentPresenter />
                  <ListBox Name="Childs" Visibility="Collapsed">
                    <TextBlock Text="Child1" />
                    <TextBlock Text="Child2" />
                    <TextBlock Text="Child3" />
                  </ListBox>
                 </StackPanel>
                <ControlTemplate.Triggers>
                  <Trigger Property="IsSelected" Value="True">
                    <Setter TargetName="Childs" Property="Visibility" Value="Visible" />
                  </Trigger>
                </ControlTemplate.Triggers>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      
      </ListView.ItemContainerStyle>        
      <TextBlock Text="1" />
      <TextBlock Text="2" />
      <TextBlock Text="3" />
    </ListView>
  </Grid>
</Page>





Regards

Joseph Leung



Regards
Joseph Leung


这篇关于wpf中的父子网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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