2011-04-03 41 views
1

我使用fluent.codeplex.com的Fluent。WPF Fluent中的圖標綁定問題

此作品在我的XAML文件:

<Image Name="image1" Stretch="Fill" Height="40" 
     HorizontalAlignment="Left"   
     VerticalAlignment="Top" Width="40" 
     Source="{StaticResource error_button}" /> 

並顯示圖像。

當我嘗試使用它作爲一個圖標按鈕流利

<Fluent:Button Header="adsfasf"> 
    <Fluent:Button.Icon> 
    <Image Height="40" HorizontalAlignment="Left" VerticalAlignment="Top" Width="40"  
     Source="{StaticResource error_button}" /> 
    </Fluent:Button.Icon> 
</Fluent:Button> 

按鈕沒有圖標。我錯過了什麼嗎?

+0

你能告訴你如何定義error_button資源? – CodeNaked 2011-04-03 00:28:49

回答

3

我不熟悉的流利色帶組件,但是通過源代碼看,它使用此,以顯示按鈕圖標:

http://fluent.codeplex.com/SourceControl/changeset/view/57318#527240

<ContentPresenter 
    ... 
    Content="{Binding LargeIcon, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource StringToImageConvert}}" /> 

StringToImageConvert定義爲:

<Fluent:ObjectToImageConverter x:Key="StringToImageConvert"/> 

該轉換器中Convert方法的實現如下所示(通過反射器 - Fluent開發人員需要分離類進入自己的文件更好)。

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
{ 
    if (value is string) 
    { 
     Image image = new Image(); 
     image.Stretch = Stretch.None; 
     image.Source = new BitmapImage(new Uri(value as string, UriKind.RelativeOrAbsolute), new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore)); 
     return image; 
    } 
    if (value is ImageSource) 
    { 
     Image image2 = new Image(); 
     image2.Stretch = Stretch.None; 
     image2.Source = (ImageSource) value; 
     return image2; 
    } 
    return value; 
} 

所以從代碼來看,最好的辦法是嘗試這樣的:

<Fluent:Button 
    Header="adsfasf" 
    Icon="{StaticResource error_button}" 
    LargeIcon="{StaticResource error_button}" 
    />