WPF CustomControl and Image binding
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)
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;
}
精彩评论