如何将图像添加到我的GridViewColumnHeader

时间:2018-01-18 12:56:21

标签: wpf listview

所以我有GridViewColumnHeader

<GridViewColumnHeader Content="File name" Click="GridViewColumnHeader_Click" />

现在我想添加image,所以我试试这个:

<GridViewColumnHeader Click="GridViewColumnHeader_Click">
     <StackPanel Orientation="Horizontal" Margin="0,0,0,0">
         <TextBlock  Text="File name"
                     Margin="0,0,0,0"/>
         <Image Width="18"
                Height="18"
                Source="pack://application:,,,/Resources/sort_ascend.ico"
                Margin="2,0,0,0"/>
      </StackPanel>
</GridViewColumnHeader>

结果是现在我看不到我的Header text而不是Image ..

有什么建议吗?

编辑:

<GridViewColumn.CellTemplate>
                                                        <DataTemplate>
                                                            <DataTemplate.Resources>
                                                                <ToolTip x:Key="Tip">
                                                                    <TextBlock>
                                                                        <Run Text="File name:   "/>
                                                                        <Run Text="{Binding FileName}"/>
                                                                        <LineBreak/>
                                                                        <Run Text="File format: "/>
                                                                        <Run Text="{Binding FileFormat}"/>
                                                                        <LineBreak/>
                                                                        <Run Text="Packets:      "/>
                                                                        <Run Text="{Binding Packets, StringFormat={}{0:#,0}}"/>                                                                        
                                                                        <LineBreak/>
                                                                        <Run Text="Duration:    "/>
                                                                        <Run Text="{Binding Duration}"/>
                                                                    </TextBlock>
                                                                </ToolTip>
                                                            </DataTemplate.Resources>
                                                            <StackPanel Orientation="Horizontal" ToolTip="{StaticResource Tip}">
                                                                <Image  Width="12"
                                                                        Height="12"
                                                                        x:Name="myImage"
                                                                        Margin="0,0,0,0"/>
                                                                <Image Width="18"
                                                                       Height="18"
                                                                       Source="pack://application:,,,/Resources/wireshark_6.ico"
                                                                       Margin="5,0,0,0"/>
                                                                <TextBlock x:Name="fileNameTextBlock"
                                                                           Text="{Binding File}"
                                                                           Style="{StaticResource TextBlockDefaultStyle}"
                                                                           Margin="5,0,0,0"/>
                                                            </StackPanel>
                                                            <DataTemplate.Triggers>
                                                                <DataTrigger Binding="{Binding BadCheckSumExist}" Value="False">
                                                                    <Setter TargetName="myImage" Property="Source" Value="pack://application:,,,/Resources/checksum_ok3.png"/>
                                                                    <Setter TargetName="myImage" Property="ToolTip" Value="Checksum OK"/>
                                                                </DataTrigger>
                                                                <DataTrigger Binding="{Binding BadCheckSumExist}" Value="True">
                                                                    <Setter TargetName="myImage" Property="Source" Value="pack://application:,,,/Resources/bad_checksum.png"/>
                                                                    <Setter TargetName="myImage" Property="ToolTip" Value="Bad checksum found"/>
                                                                </DataTrigger>
                                                            </DataTemplate.Triggers>
                                                        </DataTemplate>
                                                    </GridViewColumn.CellTemplate>

1 个答案:

答案 0 :(得分:0)

由于GridViewColumnHeaderButton,您必须将图片添加到其Content

<GridViewColumnHeader>
    <GridViewColumnHeader.Content>
     <StackPanel Orientation="Horizontal" Margin="0,0,0,0">
        <TextBlock  Text="File name"
                    Margin="0,0,0,0"/>
        <Image Width="18"
            Height="18"
            Source="pack://application:,,,/Resources/sort_ascend.ico"
            Margin="2,0,0,0"/>
      </StackPanel>
    </GridViewColumnHeader.Content>
  </GridViewColumnHeader>