我在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}" />
任何想法嗎? 非常感謝,
我想我回答很快在這裏,你想通過一個setter設置屬性,然後綁定到它的模板中? –
該視圖表示這樣一個工具欄的四個按鈕:http://sl.venuscloud.com/我想通過附加的屬性注入圖標到每個按鈕從視圖到模板的路徑。希望這更清楚。謝謝 – Houman