如何删除MyToolkit Datagrid的标题?

时间:2016-07-19 12:56:44

标签: c# .net uwp windows-10-universal

我在我的Windows应用程序https://github.com/MyToolkit/MyToolkit/wiki/DataGrid中使用MyToolkit DataGrid。我想从datagrid中删除标头。我怎样才能做到这一点? 可以任何人告诉我...... 在此先感谢。

这是我的代码

                 

        <controls:DataGrid.RowStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Padding" Value="0"></Setter>
                <Setter Property="Height" Value="35"></Setter>
                <Setter Property="VerticalAlignment" Value="Top"></Setter>
            </Style>
        </controls:DataGrid.RowStyle>

        <controls:DataGrid.Columns>

            <controls:DataGridTextColumn Binding="{Binding SNumber}" Width="0.5*" CanSort="False" Foreground="White">
                <!--<controls:DataGridTextColumn.Header>
                    <Button x:Name="btnSl" x:Uid="RSNO" Style="{StaticResource ButtonStyle1}" Background="DarkTurquoise" Margin="0,0,0,0" ></Button>
                </controls:DataGridTextColumn.Header>-->
                <controls:DataGridTextColumn.Style>
                    <Style TargetType="TextBlock">
                        <Setter Property="TextAlignment" Value="Center"></Setter>
                        <Setter Property="VerticalAlignment" Value="Center"></Setter>
                    </Style>
                </controls:DataGridTextColumn.Style>
            </controls:DataGridTextColumn>

            <controls:DataGridTemplatedColumn CellTemplate="{StaticResource myCellTemplateMonth}"  Width="2.5*" x:Name="ItemDesc"  CanSort="False" IsAscendingDefault="True">

            </controls:DataGridTemplatedColumn>

            <controls:DataGridTextColumn Binding="{Binding uom}" Width="0.5*" CanSort="False" Foreground="White">
                <!--<controls:DataGridTextColumn.Header>
                    <Button x:Name="btnUOM" x:Uid="uom" Style="{StaticResource ButtonStyle1}" Grid.Column="2" Background="DarkTurquoise"></Button>
                </controls:DataGridTextColumn.Header>-->
                <controls:DataGridTextColumn.Style>
                    <Style TargetType="TextBlock">
                        <Setter Property="TextAlignment" Value="Center"></Setter>
                        <Setter Property="Margin" Value="0,0,0,0"></Setter>
                    </Style>
                </controls:DataGridTextColumn.Style>
            </controls:DataGridTextColumn>

            <controls:DataGridTemplatedColumn  Width="0.7*" CanSort="False">
                <!--<controls:DataGridTextColumn.Header>
                    <Button x:Name="bPrice" x:Uid="ItemPrice" Style="{StaticResource ButtonStyle1}" Grid.Column="3" Background="DarkTurquoise"></Button>
                </controls:DataGridTextColumn.Header>-->
                <controls:DataGridTemplatedColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Name="pricePanel"  Height="30" Tapped="pricePanel_Tapped" >
                            <TextBlock Name="price" Foreground="White"  Text='{Binding editedPrice}' TextAlignment="Center" VerticalAlignment="Center"  Margin="0,5,0,0"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </controls:DataGridTemplatedColumn.CellTemplate>
            </controls:DataGridTemplatedColumn>

            <controls:DataGridTemplatedColumn  Width="0.7*" CanSort="False">

                <controls:DataGridTemplatedColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Name="quantityPanel"  Height="30" Tapped="quantityPanel_Tapped">
                            <TextBlock Name="quantity" TextAlignment="Center" Foreground="White"  Text='{Binding quantity}' VerticalAlignment="Center"  Margin="0,5,0,0"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </controls:DataGridTemplatedColumn.CellTemplate>
            </controls:DataGridTemplatedColumn>

            <controls:DataGridTemplatedColumn  Width="0.6*" CanSort="False">
                <!--<controls:DataGridTextColumn.Header>
                    <Button x:Name="btnDiscnt" x:Uid="Rdiscount" Style="{StaticResource ButtonStyle1}" Grid.Column="5" Background="DarkTurquoise" ></Button>
                </controls:DataGridTextColumn.Header>-->
                <controls:DataGridTemplatedColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Name="discountPanel"  Height="30" Tapped="discountPanel_Tapped" >
                            <TextBlock Name="Discount" TextAlignment="Center" Foreground="White" Text='{Binding discount}' VerticalAlignment="Center" Margin="10,5,0,0"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </controls:DataGridTemplatedColumn.CellTemplate>
            </controls:DataGridTemplatedColumn>

            <controls:DataGridTextColumn Binding="{Binding cartTotal}" Width="0.85*" CanSort="False" Foreground="White">
                <!--<controls:DataGridTextColumn.Header>
                    <Button x:Name="btnCost" x:Uid="ItemCost" Style="{StaticResource ButtonStyle1}" Grid.Column="6" Background="DarkTurquoise"></Button>
                </controls:DataGridTextColumn.Header>-->
                <controls:DataGridTextColumn.Style>
                    <Style TargetType="TextBlock">
                        <Setter Property="HorizontalAlignment" Value="Center"></Setter>
                        <Setter Property="Margin" Value="-40,0,0,0"></Setter>
                    </Style>
                </controls:DataGridTextColumn.Style>
            </controls:DataGridTextColumn>

            <!--delete image column-->
            <controls:DataGridTemplatedColumn CanSort="False">
                <!--<controls:DataGridTemplatedColumn.Header>
                    <Image Source="/Images/erase.png" Height="40" Width="40" Grid.Column="7" Margin="5"></Image>
                </controls:DataGridTemplatedColumn.Header>-->
                <controls:DataGridTemplatedColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel x:Name="voidImagePanel"  Height="30" Tapped="voidImagePanel_Tapped">
                            <Image x:Name="VoidImage" Source="/Images/delete.png" Height="20" Width="30" VerticalAlignment="Center" HorizontalAlignment="Left"  Margin="-20,5,0,0"></Image>
                        </StackPanel>
                    </DataTemplate>
                </controls:DataGridTemplatedColumn.CellTemplate>
            </controls:DataGridTemplatedColumn>


        </controls:DataGrid.Columns>
    </controls:DataGrid>
</ScrollViewer>

这里我不想要标题。但我得到空的空间。我该如何删除它。enter image description here

1 个答案:

答案 0 :(得分:2)

  

这里我不想要标题。但我得到空的空间。我怎么能删除它。

我已经检查了MyToolkit的源代码,唯一的方法是修改DataGrid的默认模板,并将ColumnHeaders元素的Visibility属性设置为Collapsed

  • 首先,添加这两个xaml前缀:

    xmlns:controls="using:MyToolkit.Controls" xmlns:Converters="using:MyToolkit.Converters"

  • 其次,附加以下样式并分配给DataGrid:

    <controls:DataGrid.Resources>
            <Converters:VisibilityConverter x:Key="vc" />
    
            <Converters:NotConverter x:Key="nc" />
    
            <!-- TransparentListBox -->
            <Style x:Key="TransparentListBox" TargetType="ListBox">
                <Setter Property="Foreground" Value="{StaticResource ListBoxForegroundThemeBrush}"/>
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="BorderBrush" Value="{StaticResource ListBoxBorderThemeBrush}"/>
                <Setter Property="BorderThickness" Value="{StaticResource ListBoxBorderThemeThickness}"/>
                <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
                <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
                <Setter Property="ScrollViewer.HorizontalScrollMode" Value="Disabled"/>
                <Setter Property="ScrollViewer.IsHorizontalRailEnabled" Value="True"/>
                <Setter Property="ScrollViewer.VerticalScrollMode" Value="Enabled"/>
                <Setter Property="ScrollViewer.IsVerticalRailEnabled" Value="True"/>
                <Setter Property="ScrollViewer.ZoomMode" Value="Disabled"/>
                <Setter Property="IsTabStop" Value="False"/>
                <Setter Property="TabNavigation" Value="Once"/>
                <Setter Property="FontFamily" Value="{StaticResource ContentControlThemeFontFamily}"/>
                <Setter Property="FontSize" Value="{StaticResource ControlContentThemeFontSize}"/>
                <Setter Property="ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel/>
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBox">
                            <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Background="{TemplateBinding Background}">
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Normal"/>
                                        <VisualState x:Name="Disabled">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="LayoutRoot">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxDisabledForegroundThemeBrush}"/>
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                    <VisualStateGroup x:Name="FocusStates">
                                        <VisualState x:Name="Focused"/>
                                        <VisualState x:Name="Unfocused"/>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <ScrollViewer x:Name="ScrollViewer" 
                                  HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}" 
                                  HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                                  IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}" 
                                  IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}" 
                                  Padding="{TemplateBinding Padding}" TabNavigation="{TemplateBinding TabNavigation}" 
                                  VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" 
                                  VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}" 
                                  ZoomMode="{TemplateBinding ScrollViewer.ZoomMode}">
                                    <ItemsPresenter/>
                                </ScrollViewer>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    
            <Style x:Key="DataGridStyle1" TargetType="controls:DataGrid">
                <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
                <Setter Property="HeaderBackground" Value="{ThemeResource SystemControlHighlightChromeHighBrush}" />
                <Setter Property="RowBackgroundOddBrush" Value="{ThemeResource SystemControlPageBackgroundChromeLowBrush}" />
                <Setter Property="CellTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <ContentPresenter Margin="12" Content="{Binding Control}" />
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="controls:DataGrid">
                            <Grid Background="{TemplateBinding Background}">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
    
                                <Grid Grid.Row="0" Visibility="Collapsed" Background="{TemplateBinding HeaderBackground}" Height="40" x:Name="ColumnHeaders">
                                    <!-- HACK: Needed so that column DPs are working when adding columns in code only. -->
                                    <ContentPresenter>
                                        <controls:DataGridTextColumn />
                                    </ContentPresenter>
                                </Grid>
    
                                <controls:MtListBox BorderThickness="0" Grid.Row="1"
                                        ItemContainerStyle="{TemplateBinding RowStyle}"
                                        HorizontalContentAlignment="Stretch" 
                                        VerticalContentAlignment="Stretch"
                                        Foreground="{ThemeResource SystemControlForegroundBaseHighBrush}"
                                        Style="{StaticResource TransparentListBox}" 
                                        Margin="0" x:Name="Rows" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Grid Background="Transparent">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
    
                                <ContentPresenter Grid.Column="0" 
                                        Margin="12,0,12,2" 
                                        VerticalAlignment="Center" 
                                        FontSize="{ThemeResource TextStyleLargeFontSize}" 
                                        Content="{Binding Header}" />
    
                                <StackPanel Grid.Column="1" 
                                        Visibility="{Binding IsSelected, Converter={StaticResource vc}}" 
                                        VerticalAlignment="Center" 
                                        HorizontalAlignment="Left">
                                    <Path Data="M4,0 L0,8 L8,8 Z" Fill="White" Visibility="{Binding IsAscending, Converter={StaticResource vc}}"/>
                                    <Path Data="M0,0 L4,8 L8,0 Z" Fill="White" Visibility="{Binding IsAscending, Converter={StaticResource nc}}"/>
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </controls:DataGrid.Resources>
        <controls:DataGrid.Style>
            <StaticResource ResourceKey="DataGridStyle1"/>
        </controls:DataGrid.Style>
    

<强>截图: enter image description here

查看我已完成的演示here