Dot-Net

如何在樣式中使用附加屬性?

  • August 21, 2011

我在 ButtonStyle 中創建了一個圖像。現在我創建了一個附加屬性,以便我可以為該圖像設置源。應該直截了當,但我堅持下去。

這是我縮短的 ButtonStyle:

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

這是附加的屬性定義,請注意,我不知道如何修復回調,因為依賴屬性似乎是按鈕而不是圖像。並且 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}" />

請問有什麼想法嗎?非常感謝,

以下是如何以樣式設置附加屬性

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

綁定到附加屬性時,路徑應在括號內,因此請嘗試使用Binding RelativeSourcewithTemplatedParent

<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顯示在執行時,但它在設計器中失敗並出現異常。您可以在 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;
}

引用自:https://stackoverflow.com/questions/7139641