4

I have a template in app.xaml. During runtime, i want to create a button and apply this template. I also want to set the Image source during runtime.

<Application.Resources>
        <ControlTemplate x:Key="btemp2" TargetType="{x:Type Button}">
            <Image x:Name="myimage" HorizontalAlignment="Center" Height="84" VerticalAlignment="Center" Width="100" Margin="10,-17.5,7,-24.5"  Stretch="UniformToFill"/>
        </ControlTemplate>
    </Application.Resources>

runtime code:

Button newButton = new Button();
newButton.Width = 100;
newButton.Height = 50;
newButton.Template = (ControlTemplate)TryFindResource("btemp2");
System.Windows.Controls.Image i = newButton.Template.FindName("myimage",this) as System.Windows.Controls.Image;

Bitmap bmp = GetIconImageFromFile(fileName);
BitmapSource src = GetBitmapImageFromBitmap(bmp);
i.Source = src;
stack.Children.Add(newButton);

It does not work as expected. Breakpoint does not reach

Bitmap bmp = GetIconImageFromFile(fileName);
3
  • 1
    so let us know what is your problem. Commented Dec 15, 2015 at 7:24
  • 1
    It doesnt work lol. Breakpoint doesnt reach Bitmap bmp = GetIconImageFromFile(fileName); Commented Dec 15, 2015 at 7:50
  • Then put it into your question so ppl can read it and directly see what your problem is. Commented Dec 15, 2015 at 7:52

2 Answers 2

4

You can use Binding to set image. So you should change ControlTemplate. In that example we using Button Tag property to set image Source.

<ControlTemplate x:Key="btemp2" TargetType="{x:Type Button}">
    <Image x:Name="myimage" HorizontalAlignment="Center" Height="84" VerticalAlignment="Center" Width="100" Margin="10,-17.5,7,-24.5"  Stretch="UniformToFill"
                    Source="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Tag}"/>
</ControlTemplate>

And the Button creating code should look like this.

Button newButton = new Button();
newButton.Width = 100;
newButton.Height = 50;
newButton.Template = ( ControlTemplate )TryFindResource( "btemp2" );
tempGrid.Children.Add( newButton );
BitmapImage image = new BitmapImage(new Uri("pack://application:,,,/WPFTest;component/Images/GPlus.png"));
newButton.Tag = image;
Sign up to request clarification or add additional context in comments.

5 Comments

Binding sould be the way to go.
Thanks so much. Finally works. Is there any recommendation books to read on wpf with c#?
You're welcome. "Wpf unleashed" is very helpful book.
In code behind, a WPF Resource File Pack URI should be created like this: new Uri("pack://application:,,,/WPFTest;component/Images/GPlus.png").
I also, as no UriKind is necessary.
1

Remove this , and use newButton in code below, and handle Loaded event :

    Grd.Children.Add(newButton);
    newButton.Loaded += newButton_Loaded;
    ...


void newButton_Loaded(object sender, RoutedEventArgs e)
        {
            Image img = (Image)newButton.Template.FindName("myimage", newButton);
            ...
        }

1 Comment

This will return null insteadof throwing an exception. So still he can't set the source on that.

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.