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 891037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:49:40+00:00 2026-05-15T13:49:40+00:00

Well it’s not really very dynamic, at least it won’t change at runtime. The

  • 0

Well it’s not really very dynamic, at least it won’t change at runtime.

The idea is I have buttons and each one has a unique image ( icon 32×32 ). The buttons all share a style where I mess with the ControlTemplate. So each image also has 2 colors one normal and another when I mouse over.

I noticed that when I declare the source path for the images is that they are almost all the same so I though DRY ( Don’t Repeat Yourself ). What if I could use the button Name or some other property as part of the source path ( i.e. the name of the image file ). That would be good programming.

Problem is I’m new to XAML, WPF and perhaps programming all together so I’m not sure how to do that. I guess this would need code behind or a converter of some sort ( guess I’ll read about converters a bit more ). Here is a bit of code ( this doesn’t work but it gives you the general idea ( hopefully )):

<Style x:Key="ButtonPanelBigButton" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border Name="ButtonBorder"
                        Height="78"
                        MaxWidth="70"
                        MinWidth="50"
                        BorderThickness="0.5"
                        BorderBrush="Transparent"
                        CornerRadius="8" >
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="2*" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>

                        <!-- Here I wan't to put in the Name property of the button because there is a picture there to match -->
                        <Image x:Name="ButtonIcon" Source="..\Images\Icons\32x32\Blue\{Binding Name}.png" 

                               Margin="4"
                               Height="32"
                               Width="32"
                               HorizontalAlignment="Center" 
                               VerticalAlignment="Center" />

                        <TextBlock Grid.Row="1"
                                   Padding="5,2,5,2"
                                   TextWrapping="Wrap"
                                   Style="{StaticResource MenuText}"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center">
                            <ContentPresenter ContentSource="Content" />                
                        </TextBlock>
                    </Grid>
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True" >
                        <Setter TargetName="ButtonIcon" Property="Source" Value="..\Images\Icons\32x32\Green\user.png" /> <!-- Same Here -->
                        <Setter TargetName="ButtonBorder" Property="BorderBrush" Value="{StaticResource SecondColorBrush}" />
                        <Setter TargetName="ButtonBorder" Property="Background">
                            <Setter.Value>
                                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" Opacity="0.5">
                                    <GradientStop Color="{StaticResource MainColor}" Offset="1" />
                                    <GradientStop Color="{StaticResource SecondColor}" Offset="0.5" />
                                    <GradientStop Color="{StaticResource MainColor}" Offset="0" />
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Hopefully you get where I’m going with this and someone might be able to help me so my code is nice and DRY ( now I’M repeating myself!!! ).

  • 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-15T13:49:40+00:00Added an answer on May 15, 2026 at 1:49 pm

    You’re right: The easy way to solve this is to use a converter.

    The Source property takes an ImageSource, so you’ll need to load the bitmap yourself in your converter.

    The converter is used like this:

     <Image Source="{Binding Name,
                             RelativeSource={RelativeSource TemplatedParent},
                             Converter={x:Static local:ImageSourceLoader.Instance},
                             ConverterParameter=..\Images\Icons\32x32\Blue\{0}.png}" />
    

    And implemented like this:

    public class ImageSourceLoader : IValueConverter
    {
      public static ImageSourceLoader Instance = new ImageSourceLoader();
    
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
        var path = string.Format((string)parameter, value.ToString());
        return BitmapFrame.Create(new Uri(path, UriKind.RelativeOrAbsolute));
      }
    
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
        throw new NotImplementedException();
      }
    }
    

    Note that this simple solution can’t handle relative Uris because the BaseUri property of the Image is unavailable to the converter. If you want to use relative Uris, you can do this by binding an attached property and using StringFormat:

     <Image local:ImageHelper.SourcePath="{Binding Name,
                             RelativeSource={RelativeSource TemplatedParent},
                             StringFormat=..\Images\Icons\32x32\Blue\{0}.png}" />
    

    And the attached property’s PropertyChangedCallback handles loads the image after combining the BaseUri with the formatted Uri string:

    public class ImageHelper : DependencyObject
    {
      public static string GetSourcePath(DependencyObject obj) { return (string)obj.GetValue(SourcePathProperty); }
      public static void SetSourcePath(DependencyObject obj, string value) { obj.SetValue(SourcePathProperty, value); }
      public static readonly DependencyProperty SourcePathProperty = DependencyProperty.RegisterAttached("SourcePath", typeof(string), typeof(ImageHelper), new PropertyMetadata
      {
        PropertyChangedCallback = (obj, e) =>
          {
            ((Image)obj).Source =
              BitmapFrame.Create(new Uri(((IUriContext)obj).BaseUri, (string)e.NewValue));
          }
      });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, this is my first post here and really enjoying the site. I have
well i have this messages table with sample values like these: msg_id recipient_id read
Well, what is one?
Well the subject is the question basically. Are there any version control systems out
Well, i've got a nice WPF book its called Sams Windows Presentation Foundation Unleashed.
Well behaved windows programs need to allow users to save their work when they
Well, after a long time writing .net programs in C# I started to feel
Well... simple question, right? But with no so simple answers. In firefox i use
Well, it seems simple enough, but I can't find a way to add a
Well this is incredibly frustrating. After being nagged by Rails that I need to

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.