简体   繁体   English

为什么我的 DataGrid 添加了一个奇怪的 HeaderColumn?

[英]Why does my DataGrid add a strange HeaderColumn?

So I'm trying to customize a DataGrid control and for some reason it adds the name of the DataContext as a HeaderColumn, why is that?所以我正在尝试自定义一个DataGrid控件,由于某种原因,它将 DataContext 的名称添加为 HeaderColumn,这是为什么呢? You can see it at the end of this picture.你可以在这张图片的最后看到它。

在此处输入图像描述

<Window x:Class="Watcher.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" 
        Height="450" Width="1200"
        Background="#27292D">

    <Window.DataContext>
        <viewmodel:MainViewModel/>
    </Window.DataContext>

..

<DataGrid Grid.Column="1"
          Grid.Row="1"
          AutoGenerateColumns = "False"
          ItemsSource="{Binding Coins}"
          GridLinesVisibility="Horizontal"
          HorizontalGridLinesBrush="Gray"
          Background="Transparent"
          HeadersVisibility="Column"
          Margin="10,0,0,0"
          BorderThickness="0"
          CanUserAddRows="False">

    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="Background" Value="Transparent"/>
        </Style>
    </DataGrid.RowStyle>

    <DataGrid.Resources>
        <Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" 
               TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="Foreground" Value="LightGray"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border BorderThickness="0,0,0,1" BorderBrush="Gray">
                            <TextBlock Text="{Binding }" FontFamily="Fonts/#Poppins" Width="120" Margin="0,0,0,5" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.Resources>

    <DataGrid.Columns>
        <DataGridTextColumn Header = "Symbol">
            <DataGridTextColumn.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <TextBlock Text="{Binding Symbol}"
                                               Width="50" Height="50"
                                               Foreground="#3C82EA"
                                               VerticalAlignment="Center"
                                               HorizontalAlignment="Left"
                                               FontFamily="Fonts/#Poppins"
                                               Padding="0,16,0,0"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Header = "Name">
            <DataGridTextColumn.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>

                                <TextBlock Text="{Binding Name}"
                                               Width="50" Height="50"
                                               Foreground="#3C82EA"
                                               VerticalAlignment="Center"
                                               HorizontalAlignment="Left"
                                               FontFamily="Fonts/#Poppins"
                                               Padding="0,16,0,0"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Header = "Price">
            <DataGridTextColumn.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>

                                <TextBlock Text="{Binding Price, StringFormat=${0}}"
                                           Width="50" Height="50"
                                           Foreground="Gray"
                                           VerticalAlignment="Center"
                                           HorizontalAlignment="Left"
                                           FontFamily="Fonts/#Poppins"
                                           Padding="0,16,0,0"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Header = "% Change (24 hrs)">



            <DataGridTextColumn.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>

                                <Border Height="25" Background="#3A514D" 
                                        HorizontalAlignment="Left"
                                        CornerRadius="4">
                                    <TextBlock Text="+58.63%" 
                                       Foreground="#4BE299" 
                                       VerticalAlignment="Center" 
                                       Padding="6,6,8,6"
                                       Margin="0,1,0,0"
                                       FontFamily="Fonts/#Poppins"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGridTextColumn.CellStyle>




        </DataGridTextColumn>
    </DataGrid.Columns>

</DataGrid>

You should use TemplateBinding for the column header style:您应该对列 header 样式使用TemplateBinding

<Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" 
           TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="Foreground" Value="LightGray"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                    <Border BorderThickness="0,0,0,1" BorderBrush="Gray">
                        <!-- Make change the line below -->
                        <TextBlock Text="{TemplateBinding Content}" FontFamily="Fonts/#Poppins" Width="120" Margin="0,0,0,5" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM