2010-04-12 54 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的類,其中我定義了像