2013-03-19 40 views
0

我有一列在我的datagrid包含圖標。 爲此我有一個單元模板以編程方式添加到列。設置圖標高度圖像模板編程

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

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

UPDATE試試這個

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

參考 See this

+0

我已經嘗試過上面的設置HeightPrtoperty。但仍然不起作用 – deathrace 2013-03-19 09:10:06

+0

實際上,我的問題是圖標圖像將在執行期間隨時加載。所以我現在只是在加載時有FrameworkElementFactory。現在,我試圖setimageFactory.SetValue(FrameworkElement.HeightProperty,height);但它不起作用。它動態地裁剪圖像圖標。 – deathrace 2013-03-19 12:24:55