我在app.xaml中有一个模板。在运行时,我想创建一个按钮并应用此模板。我还想在运行时设置图像源。
<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>
运行时代码:
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);
它没有按预期工作。 断点没有达到。
Bitmap bmp = GetIconImageFromFile(fileName);