views:

241

answers:

1

Hi all,

I am really getting mad since 2 days with a stupid problem. I already have asked the question here but seem like my question in lost where nobody will see it again. So here is my simple problem :

I have a project containing a CustomControl (a library project), this custom control code is inherited from the Window control. So it have a Icon property inherited from it. In the XAML code to create the control design, somewhere in my ResourceDictionary I want to place an Image binded to the Icon property.

...    
<Image Grid.Column="0" Margin="3" Width="27" Height="27" Source="{Binding Icon}" />
...

I have then a second project (a WPF application project) referencing my first one and using this custom control window, where I set the Icon property. The icon Property is correctly set cause I can see the icon in the task bar, but the Image doesn't appear, seem like my binding doesn't work.

<SILU:FlatForm x:Class="SILU_MovieManager.WinMain"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:SILU="clr-namespace:SILU_Controls;assembly=SILU_Controls"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SILU Movie Manager" Height="425" Width="682" Loaded="FlatForm_Loaded" Icon="/SILU_MovieManager;component/Resources/Images/Film.ico">
    <Grid>

    </Grid>
</SILU:FlatForm>

I really don't know how to bind this, here is one solution I got here, but it doesn't work for me. (Solution)

A: 

I haven't tried this solution and this done through code and for Icons

<Window x:Class="WPFWindowAPP.IconLoader"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="WPFWindowAPP" Height="164" Width="405"

>

<Canvas>


    <Button Name="btn" Click="btnClick" Canvas.Top="40" Canvas.Right="90" Width="75">Load Icon</Button>

    <Image Name="icoDisplay" Canvas.Left="10" Canvas.Top="80" Stretch="None" />

</Canvas>

    void btnClick(object sender, RoutedEventArgs e)        {

IconImage ico = IconImage.ExtractAssociatedIcon(filePath.Text); Bitmap bmp = ico.ToBitmap(); MemoryStream strm = new MemoryStream(); bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Png);
BitmapImage bmpImage = new BitmapImage();
bmpImage.BeginInit();
strm.Seek(0, SeekOrigin.Begin); bmpImage.StreamSource = strm; bmpImage.EndInit();
icoDisplay.Source = bmpImage; }

Kishore Kumar
This is done with a file path, my icon is a resource from the WPF application, I can't apply this method (I think).
Karnalta
I found one solution but i don't know this will suite your case but anyway try this. Add the icon to the project as a Resource (not Embedded Resource, there is a difference), then access it like thisusing (Stream stream = Application.GetResourceStream(new Uri("\\comment.ico",UriKind.RelativeOrAbsolute)).Stream) { BitmapDecoder decoder = IconBitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.None); BitmapSource source = decoder.Frames[0]; this.test.Source = source ; }
Kishore Kumar
Thank but I am in a Custom Control Library, I can't do statement like this.MyImage.Source. I have a class inherited from Window class and a ResourceDictionary where I define the control style like <Style TargetType="{x:Type SILU:FlatForm}"> ...
Karnalta