24

我在ButtonStyle中創建了一個圖像。現在我創建了一個附加屬性,以便可以爲該圖像設置源。應該直截了當,但我堅持下去。如何在樣式中使用附加屬性?

這是我的縮短的ButtonStyle:

<Style x:Key="ToolBarButtonStyle" 
     TargetType="Button"> 
    ... 
    <Image x:Name="toolbarImage" 
      Source="{TemplateBinding PrismExt:ImageSourceAttachable:ImageSource}" 
      Width="48" 
      Height="48" /> 
    ... 
</Style> 

這是附加屬性的定義,請注意,我不知道如何解決回調,作爲DependencyProperty的似乎是按鈕而不是圖像。並且Button不會在其樣式中顯示我的圖像。這很棘手。

namespace SalesContactManagement.Infrastructure.PrismExt 
{ 
    public class ImgSourceAttachable 
    { 
     public static void SetImgSource(DependencyObject obj, string imgSource) 
     { 
      obj.SetValue(ImgSourceProperty, imgSource); 
     } 

     public static string GetImgSource(DependencyObject obj) 
     { 
      return obj.GetValue(ImgSourceProperty).ToString(); 
     } 

     // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... 
     public static readonly DependencyProperty ImgSourceProperty = 
      DependencyProperty.RegisterAttached("ImgSource", typeof(string), typeof(ImgSourceAttachable), new PropertyMetadata(Callback)); 

     private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      //((Button)d).Source = new BitmapImage(new Uri(Application.Current.Host.Source, e.NewValue.ToString())); 
     } 
    } 
} 

這是我如何設置XAML中的圖片來源:

<Button PrismExt:ImgSourceAttachable.ImgSource="./Images/New.png" 
     Style="{StaticResource ToolBarButtonStyle}" /> 

任何想法嗎? 非常感謝,

+0

我想我回答很快在這裏,你想通過一個setter設置屬性,然後綁定到它的模板中? –

+0

該視圖表示這樣一個工具欄的四個按鈕:http://sl.venuscloud.com/我想通過附加的屬性注入圖標到每個按鈕從視圖到模板的路徑。希望這更清楚。謝謝 – Houman

回答

37

這裏是你如何設置你的附加屬性的風格

<Style x:Key="ToolBarButtonStyle" TargetType="Button"> 
    <Setter Property="PrismExt:ImgSourceAttachable.ImgSource" 
      Value="./Images/New.png"/> 
    <!--...--> 
</Style> 

當綁定到附加屬性則路徑應該是括號內,以便儘量使用RelativeSourceTemplatedParent代替

綁定
<Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="Button"> 
      <Image x:Name="toolbarImage" 
        Source="{Binding RelativeSource={RelativeSource TemplatedParent}, 
            Path=(PrismExt:ImgSourceAttachable.ImgSource)}" 
        Width="48" 
        Height="48"> 
      </Image> 
     </ControlTemplate> 
    </Setter.Value> 
</Setter> 

編輯:上面的代碼工作在WPF,Silverlight中運行時的Image節目,但它在T失敗他的設計師除外。您可以使用下面的代碼PropertyChangedCallback得到Image作爲一種解決方法

private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    Button button = d as Button; 
    Image image = GetVisualChild<Image>(button); 
    if (image == null) 
    { 
     RoutedEventHandler loadedEventHandler = null; 
     loadedEventHandler = (object sender, RoutedEventArgs ea) => 
     { 
      button.Loaded -= loadedEventHandler; 
      button.ApplyTemplate(); 
      image = GetVisualChild<Image>(button); 
      // Here you can use the image 
     }; 
     button.Loaded += loadedEventHandler; 
    } 
    else 
    { 
     // Here you can use the image 
    } 
} 
private static T GetVisualChild<T>(DependencyObject parent) where T : DependencyObject 
{ 
    T child = default(T); 

    int numVisuals = VisualTreeHelper.GetChildrenCount(parent); 
    for (int i = 0; i < numVisuals; i++) 
    { 
     DependencyObject v = (DependencyObject)VisualTreeHelper.GetChild(parent, i); 
     child = v as T; 
     if (child == null) 
     { 
      child = GetVisualChild<T>(v); 
     } 
     if (child != null) 
     { 
      break; 
     } 
    } 
    return child; 
} 
+0

謝謝。此代碼現在編譯,但我仍然看不到圖像。我很確定它是因爲附加屬性的回調是空的。然而,「DependencyObject d」有按鈕本身。我不明白... – Houman

+0

我剛剛注意到你的問題有WPF和Silverlight標籤,我的回答是針對WPF的。我會爲Silverlight嘗試一下。 –

+0

你說得對。道歉。我認爲關於附加屬性他們將沒有什麼不同。 :) – Houman