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