Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6349327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:35:39+00:00 2026-05-24T21:35:39+00:00

I have created an Image within a ButtonStyle. Now I have created an Attached

  • 0

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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:35:40+00:00Added an answer on May 24, 2026 at 9:35 pm

    Here is how you can set your attached property in a style

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

    When binding to attached properties then the Path should be within parentheses so try to use RelativeSource Binding with TemplatedParent instead

    <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>
    

    Edit: The above code works in WPF, in Silverlight the Image shows in runtime but it fails in the designer with an exception. You can use the following code in the PropertyChangedCallback to get the Image as a workaround

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dynamically created image that I am saving to a stream so
I have created a Windows CE6 Image through Platform Builder, and I now want
I have created an ashx handler that returns an image to my flex app.
I have created a Python file to generate a Mandelbrot set image. The original
I have an image gallery that I created by reading the contents inside a
I have an ASP.NET document, with an Image element within it. I created a
I have created a scrolling/zooming image view by placing a UIImageView within a UIScrollView.
I have created an array of coordinates. The centre of an image should move
I have created a silverlight app that consits of like buttons within specific usercontrols
I have created a XML image gallery, which displays text in between each slide.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.