WPF DataGrid展开/折叠所有代码

时间:2014-12-10 00:05:23

标签: wpf vb.net xaml

我一直在寻找解决方案的日子,但我无法相信它有多么难以证明。我在窗口中有一个数据网格。我想添加一个可以展开/折叠全部的按钮。我在后面的代码中使用VB,但我找不到任何有用的东西。

这是我的DG代码......不确定是否需要查看此代码。当窗口打开时,所有内容都会折叠。我希望用户可以选择展开单行,或者单击按钮展开所有内容。

 <DataGrid x:Name="dataGrid" FontSize="15" CanUserAddRows="false" Margin="10,106,10,10" ColumnWidth="*" RowHeight="45" ItemsSource="{Binding Items, Mode=OneWay}" AutoGenerateColumns="False" IsReadOnly="True" AlternatingRowBackground="#FF6E6E6E" Foreground="Black" BorderBrush="#FF595A5B" Background="Transparent" HorizontalGridLinesBrush="Black"  VerticalGridLinesBrush="Black" CanUserReorderColumns="False" RowHeaderWidth="0"  >
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Setter Property="Background" Value="#FF575757" />
                <Setter Property="Foreground" Value="White" />
                <Style.Triggers>
                    <!--<Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="LightGray"/>
                        <Setter Property="Foreground" Value="White"/>
                    </Trigger>-->
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="Background" Value="#FF616161" />
                <Setter Property="Foreground" Value="White" />
                <Setter Property="FontSize" Value="14"/>
            </Style>
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="DodgerBlue"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White"/>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Press" Binding="{Binding press}"></DataGridTextColumn>
            <DataGridTextColumn Header="Order Number" Binding="{Binding orderNumber}"></DataGridTextColumn>
            <DataGridTextColumn Header="Order Type" Binding="{Binding orderType}"></DataGridTextColumn>
            <DataGridTextColumn Header="Start Date" Binding="{Binding tstart, StringFormat=\{0:yyyy-MM-dd \}}" Visibility="Hidden" ></DataGridTextColumn>
            <DataGridTextColumn x:Name="dataGridTextColumn" Header="Tool" Binding="{Binding tool}"></DataGridTextColumn>
            <DataGridTextColumn Header="Order Time" Binding="{Binding ts}"></DataGridTextColumn>
            <DataGridTextColumn Header="Color No." Binding="{Binding colorno}"></DataGridTextColumn>
            <DataGridTextColumn Header="Material No." Binding="{Binding matno}"></DataGridTextColumn>
            <DataGridTextColumn Header="Quantity" Binding="{Binding qty}"></DataGridTextColumn>
            <DataGridTextColumn Header="Sequence" Visibility="Hidden" Binding="{Binding seq}"></DataGridTextColumn>
            <DataGridTextColumn Header="Employee" Binding="{Binding emp}"></DataGridTextColumn>
            <DataGridTextColumn Header="Remaining Weight" Binding="{Binding tw}"></DataGridTextColumn>
            <DataGridTextColumn Header="Total Weight"  Binding="{Binding tw, StringFormat={}{0:n2}}"></DataGridTextColumn>
            <DataGridTemplateColumn Header="Click To Select" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button x:Name="myButton4">Select Order</Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="Padding" Value="5"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.CellStyle>
        <DataGrid.GroupStyle>
            <GroupStyle x:Name="groupstyle1">
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Path=Name}" Background="Orange"/>

                        </StackPanel>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander  x:Name="myexpander1"  >
                                        <Expander.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock FontSize="22" Foreground="White" Text="{Binding Path=Name}" />

                                            </StackPanel>
                                        </Expander.Header>
                                        <ItemsPresenter />
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </DataGrid.GroupStyle>
    </DataGrid>

感谢您提供任何帮助。我真的很感激。

0 个答案:

没有答案