I have created an Image within a ButtonStyle. Now I have created an Attached Property so that I can set the Source for that Image. Should be straight forward but I am stuck with it.
This is my shortened ButtonStyle:
<Style x:Key="ToolBarButtonStyle"
TargetType="Button">
...
<Image x:Name="toolbarImage"
Source="{TemplateBinding PrismExt:ImageSourceAttachable:ImageSource}"
Width="48"
Height="48" />
...
</Style>
And this is the attached property definition, Note that I have no idea how to fix the callback, as the dependencyproperty seems to be the button instead of the image. And Button doesn't expose my Image within its style. Its tricky.
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()));
}
}
}
This is how I set the image source within XAML:
<Button PrismExt:ImgSourceAttachable.ImgSource="./Images/New.png"
Style="{StaticResource ToolBarButtonStyle}" />
Any ideas please? Many Thanks,
See Question&Answers more detail:os