Como usar a propriedade Attached dentro de um estilo?
Criei uma imagem dentro de um ButtonStyle. Agora eu criei uma propriedade anexada para que eu possa definir a fonte dessa imagem. Deve ser direto, mas estou preso a ele.
Este é o meu ButtonStyle reduzido:
<Style x:Key="ToolBarButtonStyle"
TargetType="Button">
...
<Image x:Name="toolbarImage"
Source="{TemplateBinding PrismExt:ImageSourceAttachable:ImageSource}"
Width="48"
Height="48" />
...
</Style>
E esta é a definição de propriedade anexada. Observe que não tenho idéia de como corrigir o retorno de chamada, pois a propriedade de dependência parece ser o botão em vez da imagem. E Button não expõe minha imagem dentro de seu estilo. É complicado
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()));
}
}
}
É assim que eu defino a fonte da imagem no XAML:
<Button PrismExt:ImgSourceAttachable.ImgSource="./Images/New.png"
Style="{StaticResource ToolBarButtonStyle}" />
Alguma idéia, por favor? Muito Obrigado