Dowemo
0 0 0 0


Question:

I have included an icon file inside my Resources.resx that I'd like to show on a TreeViewItem that is inside a stackpanel.

1)Can .ico files be used for this purpose? Or does it have to be .bmp or jpg?

2)What do you set the source as in XAML? The following code didn't work for me

<StackPanel Orientation="Horizontal">
    <Image Margin="2" Source="/Resources/Folder_Back.ico" />
    <TextBlock Margin="2" Text="{Binding ProgramName}"
     Foreground="White" FontWeight="Bold"/>
</StackPanel>

Best Answer:


to make the solution of Qorbani work add a converter to the Image Source.Binding!

XAML - Namespaces

 xmlns:properties="clr-namespace:YourNameSpace.Properties"
 xmlns:converter="clr-namespace:YourNameSpace.Converter"

Xaml - Resource (UserControl or Window)

 <UserControl.Resources>
        <ResourceDictionary>
              <converter:BitmapToImageSourceConverter x:Key="BitmapToImageSourceConverter" />
        </ResourceDictionary>
 </UserControl.Resources>

Xaml Code

<StackPanel Orientation="Horizontal">
                    <Image Width="32" Height="32" Source="{Binding Source={x:Static properties:Resources.Import}, Converter={StaticResource BitmapToImageSourceConverter}}" Stretch="Fill" />
                    <TextBlock Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center">Import</TextBlock>
</StackPanel>

BitmapToImageSourceConverter.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace YourNameSpace
{
    public class BitmapToImageSourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var bitmap = value as System.Drawing.Bitmap;
            if (bitmap == null)
                throw new ArgumentNullException("bitmap");
            var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            var bitmapData = bitmap.LockBits(
                rect,
                ImageLockMode.ReadWrite,
                System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            try
            {
                var size = (rect.Width * rect.Height) * 4;
                return BitmapSource.Create(
                    bitmap.Width,
                    bitmap.Height,
                    bitmap.HorizontalResolution,
                    bitmap.VerticalResolution,
                    PixelFormats.Bgra32,
                    null,
                    bitmapData.Scan0,
                    size,
                    bitmapData.Stride);
            }
            finally
            {
                bitmap.UnlockBits(bitmapData);
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}



Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs