【问题标题】:Why does my DataGrid add a strange HeaderColumn?为什么我的 DataGrid 添加了一个奇怪的 HeaderColumn?
【发布时间】:2021-05-25 20:05:27
【问题描述】:

所以我正在尝试自定义一个DataGrid 控件,并且由于某种原因它将DataContext 的名称添加为HeaderColumn,这是为什么呢?您可以在这张图片的末尾看到它。

<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>

【问题讨论】:

标签: c# .net wpf


【解决方案1】:

您应该使用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>

【讨论】:

  • 我不得不更改ControlTemplate 上的TargetType="{x:Type DataGridColumnHeader}",除此之外,它就像一个魅力。
  • 是的,我忘了复制那段代码。哈哈
猜你喜欢
  • 1970-01-01
  • 2013-09-01
  • 2018-03-13
  • 1970-01-01
  • 2012-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多