无法在WPF ListView标头中显示图像

时间:2019-04-30 19:42:13

标签: c#

我正在尝试使用以下代码创建WPF ListView HeaderTemplate

    FrameworkElementFactory HeaderStackPanelFEFactory = new 
    FrameworkElementFactory(typeof(StackPanel));

    HeaderStackPanelFEFactory.SetValue(StackPanel.OrientationProperty, 
    Orientation.Horizontal);
    FrameworkElementFactory HeaderImageFEFactory = new 
    FrameworkElementFactory(typeof(Image));
    HeaderImageFEFactory.SetValue(Image.StretchProperty, Stretch.Fill);
    HeaderImageFEFactory.SetValue(Image.SourceProperty, new BitmapImage(new 
    Uri("Resources/IM_Edit.png", UriKind.Relative)));
    FrameworkElementFactory HeaderTextBlockFEFactory = new 
    FrameworkElementFactory(typeof(TextBlock));
    HeaderTextBlockFEFactory.SetValue(TextBlock.TextProperty, ColumnHeader);
    HeaderStackPanelFEFactory.AppendChild(HeaderImageFEFactory);
    HeaderStackPanelFEFactory.AppendChild(HeaderTextBlockFEFactory);

    DataTemplate HeaderTemplate = new DataTemplate
    {
        VisualTree = HeaderStackPanelFEFactory
    };

    GridViewColumn NewColumn = new GridViewColumn
    {
    HeaderTemplate = HeaderTemplate
    };

我在模板的TextBlock部分中获得带有列名的标题。问题是标题中没有图像。当我尝试使用XAML复制相同内容时,一切正常,图像在那里。我在这里做什么错了?

0 个答案:

没有答案