我在app.xaml中有一个模板。在运行期间,我想创建一个按钮并应用此模板。我也想在运行时设置图像源。在运行时设置图像WPF
<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);
因此,让我们知道什么是你的问题。 – AntiHeadshot
它不工作大声笑。断点不会到达位图bmp = GetIconImageFromFile(fileName); – RStyle
然后把它放到你的问题中,这样ppl可以读取它并直接看到你的问题是什么。 – AntiHeadshot