我的数据网格中有一列包含图标。 为此,我以编程方式将一个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);
当我在外部设置高度属性时,裁剪图像,而不是将图像调整为“高度”值。
如何将图像高度设置为特定值。 请建议。
答案 0 :(得分:1)
试试这个
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));
REFERENCE See this
答案 1 :(得分:0)
我使用了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();