设置高度的图标图像模板编程

本文关键字:编程 图像 图标 高度 设置 | 更新日期: 2023-09-27 18:04:33

我有一个列在我的数据网格,其中包含图标。为此,我有一个单元格模板添加到列编程。

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 '值。

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

设置高度的图标图像模板编程

try this

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

UPDATE try this

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

参考看到这个

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

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();