C# 以编程方式设置图标图像模板的高度

C# 以编程方式设置图标图像模板的高度,c#,wpf,datatemplate,C#,Wpf,Datatemplate,我的datagrid中有一列包含图标。 为此,我以编程方式向列中添加了一个celltemplate var imageFactory = new FrameworkElementFactory(typeof(System.Windows.Controls.Image)); imageFactory.SetBinding(System.Windows.Controls.Image.SourceProperty, imageBinding);

我的datagrid中有一列包含图标。 为此,我以编程方式向列中添加了一个celltemplate

var imageFactory = new FrameworkElementFactory(typeof(System.Windows.Controls.Image));
                    imageFactory.SetBinding(System.Windows.Controls.Image.SourceProperty, imageBinding);
                    imageFactory.SetValue(System.Windows.Controls.Image.StretchProperty, Stretch.None);
                    if (config.Font != null)
                    {
                        double height = config.Font.Size;
                        imageFactory.SetValue(FrameworkElement.HeightProperty, height);

                    }
                    var dataTemplate = new DataTemplate { VisualTree = imageFactory };
                    statusColumn.CellTemplate = dataTemplate;
                    view.DataGrid.Columns.Add(statusColumn);
当我在外部设置Height属性时,它会裁剪图像,而不是将图像大小调整为“Height”值

如何将图像高度设置为特定值。 请建议。

试试这个

    double size = 14.0;
    BitmapImage bmp = new BitmapImage(new Uri("MyIcon.ico", UriKind.RelativeOrAbsolute));

    FrameworkElementFactory icon = new FrameworkElementFactory(typeof(Image));
    icon.SetValue(Image.SourceProperty, bmp);
    icon.SetValue(Image.WidthProperty, size);
    icon.SetValue(Image.HeightProperty, size);
更新,试试这个

   Style sBase = (Style)this.Resources["BaseButtonStyle"];
   Style sNew = new Style(typeof(Image), sBase);
   sNew.Setters.Add(new Setter(HeightProperty, 20d));
参考文献

我使用了BitmapImage.DecodePixelHeight,它解决了我的问题。:)

bitmapImage=新的bitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource=memoryStream;

bitmapImage.DecodePixelHeight=font.Size我已经尝试了上面相同的方法,设置了HeightPrtoperty。但仍然不起作用实际上我的问题是图标图像将在执行过程中随时动态加载。所以在加载时,我现在只有FrameworkElementFactory。现在,我尝试设置ImageFactory.SetValue(FrameworkElement.HeightProperty,height);但它不起作用。这是一个动态的菱形图像图标。对我有用,我的相关问题是:
bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memoryStream;
                bitmapImage.DecodePixelHeight = font.Size <= 9 ? font.Size + 2 : font.Size;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                bitmapImage.Freeze();