1

I'm very new to WPF, so please bear with me.

Basically I have defined a Style in a WPF UserControl to show buttons with an image as follows:

<UserControl.Resources>
    <Style TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <TextBlock  HorizontalAlignment="Center" VerticalAlignment="Center">
                        <Image Width="16" Height="16" Stretch="UniformToFill"/>
                        <ContentPresenter/>
                    </TextBlock>
                </ControlTemplate>
            </Setter.Value>

        </Setter>
    </Style>
</UserControl.Resources>

I then add a load of buttons to a grid at runtime (it has to be at run time as the number and type of button is dynamic).

What I would like to do is set the image of the buttons at runtime as well. I have tried a number of ways, but none seem to work. Setting the source in the Xaml works fine. The code I'm trying is as follows:

Button b = new Button();
// Create source.
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"C:\SourceCode\DSA\DSALibrary\Icons\money.png");
bi.EndInit();

b.SetValue(Image.SourceProperty, bi);

Could anybody point me towards where I'm going wrong, if I were to guess, I would say that where I think I'm setting the value, I'm actually not.

Cheers

3
  • What type is 'b'? Your UserControl derived button? Commented Aug 5, 2009 at 11:31
  • The problem is that Button is no Image.. And you are trying to set a strange property (for button) on b.. You can set the bitmap on the Content of button and use a datatemplate (see answer) Commented Aug 5, 2009 at 11:41
  • Edited to show what B. @Arcturus, I thought that might be the problem, I just wasn't sure how I would go about setting it, if datatemplates are the way to go, thanks very much. Commented Aug 5, 2009 at 11:44

2 Answers 2

3

You can try to use the Content property of Button, and declare a DataTemplate for handling the content:

<UserControl.Resources>
    <Style TargetType="Button">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Width="16" Height="16" Stretch="UniformToFill" Source="{Binding}"/>
                        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="My button text" />
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <ContentPresenter/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</UserControl.Resources>

Set the BitmapImage on the content of your button, et voila :)

Sign up to request clarification or add additional context in comments.

9 Comments

OK... I'm with you. I must admit I haven't hit binding yet. Could you point me in the direction of how I define what is "{Binding}"? Thanks
Just a quick point, I can't see how the text of the button is set in your example above?
When you set the Content on Button, the ContentControl (Button) will use a intern ContentPresenter. The ContentPresenter will set the Content as its DataSource. The DataSource property is the property which will determe the direct binding. So the {Binding} creates a binding to the DataSource, which happens to be the Content which is the Bitmap image... :)
Ahh I'm sorry.. I skimped the text.. let me add that ;)
OK, that looks good. But what if I want the text and the image to be dynamic?
|
0

Can you confirm that it's a System.Windows.Controls.UserControl derived object you're creating?

My guess would be that the control loads it's bitmap when you create it so setting the image source property after that isn't being caught. Have you tried creating a button programmatically with a two-stage creation and setting it before you do the second stage?

You could try calling the button's Invalidate() function to make sure it redraws.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.