如何隐藏DataGrid标题左侧的箭头?

时间:2014-05-21 16:22:01

标签: c# wpf datagrid

检查以下链接 http://tpstatic.com/img/usermedia/lN3his36gUeN0P0-1csn0A/original.png

我要做的是摆脱左角的箭头。问题是我是从代码而不是在XAML中创建我的DataGrid。这是代码:

DataGrid reportDataGrid = new DataGrid();
reportDataGrid.AutoGenerateColumns = false;
reportDataGrid.CanUserAddRows = false;
reportDataGrid.IsReadOnly = true;
reportDataGrid.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
reportDataGrid.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
reportDataGrid.RowHeaderWidth = 0;
reportDataGrid.HeadersVisibility = DataGridHeadersVisibility.Column;

一些列定义和绑定:

var caseNameCol= new DataGridTextColumn();
caseNameCol.Header = "Case Name";
Binding binding = new Binding("CaseName");
binding.Mode = BindingMode.OneWay;
binding.Source = list;
caseNameCol.Binding = binding;
reportDataGrid.Columns.Add(caseNameCol);

var caseItemNameCol= new DataGridTextColumn();
caseItemNameCol.Header = "Case Item Name";
Binding caseBinding = new Binding("CaseItemName");
caseBinding .Source = list;
caseBinding .Mode = BindingMode.OneWay;
caseItemNameCol.Binding = caseBinding ;
reportDataGrid.Columns.Add(caseItemNameCol);

一切正常,但问题是箭头仍然在我的数据网格中。知道如何从datagrid中删除它吗?

2 个答案:

答案 0 :(得分:2)

为这个准备下来和肮脏。

首先,我们将为datagrid定义一个ControlTemplate。这将基于默认值,只需稍作修改即可。您可以在MSDN上找到默认模板。

<Window.Resources>
    <ControlTemplate x:Key="dgControlTemplate" TargetType="{x:Type DataGrid}">
        <Border x:Name="border"
            SnapsToDevicePixels="True"
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}"
            Padding="{TemplateBinding Padding}">
            <Border.Background>
                <SolidColorBrush Color="{DynamicResource ControlLightColor}" />
            </Border.Background>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Disabled">
                        <Storyboard>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetName="border"
                                            Storyboard.TargetProperty="(Panel.Background).
                  (SolidColorBrush.Color)">
                                <EasingColorKeyFrame KeyTime="0"
                                     Value="{DynamicResource ControlLightColor}" />
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Normal" />
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <ScrollViewer x:Name="DG_ScrollViewer"
                    Focusable="false"
                    Background="Black">
                <ScrollViewer.Template>
                    <ControlTemplate TargetType="{x:Type ScrollViewer}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>

                            <Button Focusable="false"
                      Command="{x:Static DataGrid.SelectAllCommand}"

                       Loaded="theScaryButton_Loaded"  

                      Style="{DynamicResource {ComponentResourceKey 
                  ResourceId=DataGridSelectAllButtonStyle, 
                  TypeInTargetAssembly={x:Type DataGrid}}}"

                      Visibility="{Binding HeadersVisibility, 
                  ConverterParameter={x:Static DataGridHeadersVisibility.All}, 
                  Converter={x:Static DataGrid.HeadersVisibilityConverter}, 
                  RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                      Width="{Binding CellsPanelHorizontalOffset, 
                  RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>

                            <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter"
                                              Grid.Column="1"
                                              Visibility="{Binding HeadersVisibility, 
                  ConverterParameter={x:Static DataGridHeadersVisibility.Column}, 
                  Converter={x:Static DataGrid.HeadersVisibilityConverter}, 
                  RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />

                            <ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
                                      Grid.ColumnSpan="2"
                                      Grid.Row="1"
                                      CanContentScroll="{TemplateBinding CanContentScroll}" />

                            <ScrollBar x:Name="PART_VerticalScrollBar"
                         Grid.Column="2"
                         Grid.Row="1"
                         Orientation="Vertical"
                         ViewportSize="{TemplateBinding ViewportHeight}"
                         Maximum="{TemplateBinding ScrollableHeight}"
                         Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
                         Value="{Binding VerticalOffset, Mode=OneWay, 
                  RelativeSource={RelativeSource TemplatedParent}}"/>

                            <Grid Grid.Column="1"
                    Grid.Row="2">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, 
                      RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>

                                <ScrollBar x:Name="PART_HorizontalScrollBar"
                           Grid.Column="1"
                           Orientation="Horizontal"
                           ViewportSize="{TemplateBinding ViewportWidth}"
                           Maximum="{TemplateBinding ScrollableWidth}"
                           Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
                           Value="{Binding HorizontalOffset, Mode=OneWay, 
                    RelativeSource={RelativeSource TemplatedParent}}"/>
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </ScrollViewer.Template>
                <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
            </ScrollViewer>
        </Border>
    </ControlTemplate>
</Window.Resources>

我们添加的内容是:

Loaded="theScaryButton_Loaded"  

同时在我们窗口的代码中......

//set up datagrid...
this.reportDataGrid.Template = this.Resources["dgControlTemplate"] as ControlTemplate;

以及

private void theScaryButton_Loaded(object sender, RoutedEventArgs e)
{
    //this is a baddy way to do this.  better to recurse through visual tree
    //or try your luck at FindName/GetTemplateChild nonsense

    VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(sender as FrameworkElement, 0), 1).SetValue(FrameworkElement.VisibilityProperty, Visibility.Collapsed);
}

可怕的按钮里面埋藏着令人讨厌的小多边形。为了举个例子,我添加了一些baddy代码来抓住他并把他关起来。更好的方法是递归可视树,或者从Button派生,并试试GetTemplateChild。不过,这应该足以让你入门。请原谅我,如果这是非常复杂的,我来自Silverlight World,那里的一切都非常复杂。

你也可以考虑尝试通过可视树来找到令人讨厌的小多边形,但是要准备好在你的路上挖掘不是一个,而不是两个,而是三个控制模板。

答案 1 :(得分:0)

使用RowHeaderWidth属性:

<my:DataGrid RowHeaderWidth="0" AutoGenerateColumns="False" Name="dataGrid1" />

请注意,您也可以为它指定样式或模板,如果您确定自己喜欢它并希望保留它,因为您可以使用它做一些很酷的事情。