2010-04-12 58 views
0

我真的很生气,因为2天有一个愚蠢的问题。我已经在这里问过这个问题,但看起来像是我的问题迷失在没有人会再次看到它的地方。所以这里是我的简单问题:WPF CustomControl和图像绑定

我有一个项目包含一个CustomControl(一个库项目),这个自定义控制代码是从窗口控件继承。所以它有一个从它继承的Icon属性。在用于创建控件设计的XAML代码中,我想在ResourceDictionary中的某处放置一个绑定到Icon属性的图像。

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

我有那么第二个项目(一个WPF应用程序项目)引用我的第一个和使用这种自定义控件窗口,在这里我设置图标属性。图标属性设置正确,因为我可以看到任务栏中的图标,但图像未显示,看起来像我的绑定不起作用。

<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> 

我真的不知道该如何绑定这一点,这里是一个解决方案,我来到这里,但它不为我工作。 (Solution

回答

0

我还没有尝试过该溶液,并通过代码和图标

<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)这完成的; 位图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; }

+0

这是用一个文件路径完成的,我的图标是来自WPF应用程序的资源,我无法应用此方法(我认为)。 – Karnalta 2010-04-12 09:35:11

+0

我找到了一个解决方案,但我不知道这会套用你的情况,但无论如何试试这个。将图标添加到项目中作为资源(不是嵌入式资源,有差异),然后像这样访问它 使用(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; } – 2010-04-12 12:37:16

+0

谢谢,但我在一个自定义控件库中,我不能做这样的声明,像这样.MyImage.Source。我有一个继承自Window类和ResourceDictionary的类,其中我定义了像