我需要在 WPF 列标题中添加自定义图像
它只是不起作用,我得到一个空白的列标题,其中没有任何内容
我错过了什么?
<DataGrid Sorting="OnDataSort_Files" SelectionMode="Single" AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTemplateColumn Width="auto" SortMemberPath="Name">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{DynamicResource FilesTabIcon}" Width="19" Height="19"/>
<TextBlock Text="Name" Margin="4,0,0,0" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="{Binding FileTypeImageSource}" Width="16" Height="16" Margin="0,0,4,0"/>
<TextBlock Grid.Column="1" Text="{Binding Name}" MouseLeftButtonUp="OnClick_FilesItem" MouseRightButtonUp="OnClick_FilesItem" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
...
感谢您的帮助
这对我有用,所以它也应该对你有用,只需用你的图像替换我的路径:
<DataGrid ItemsSource="{Binding YourProperties}">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<Path Data="M20 14H14V20H10V14H4V10H10V4H14V10H20V14Z" Fill="HotPink"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>