将图像源从 C# 代码隐藏添加到内容控件



我正在尝试通过 C# 中的后台代码添加ContentControl,但未能通过代码将图像放入ContentControl

这是 Xaml 中的工作代码

<ContentControl x:Name="cControl" Width="200"
              Height="200"
              Canvas.Left="210"
              Canvas.Top="220"
              Style="{StaticResource DesignerItemStyle}">
      <Image IsHitTestVisible="False" Stretch="Fill" Source="pack://application:,,,../Resources/ACImages/737.png"/>
</ContentControl>

在 C# 中,到目前为止,这就是我所拥有的:

 Image ACimage = new Image();
 BitmapImage image = new BitmapImage();
 image.BeginInit();
 image.UriSource = new Uri("pack://application:,,,../Resources/ACImages/737.png");
 image.EndInit();
 ACimage.Source = image;
 ContentControl myContentControl = new ContentControl();
 Style s = this.FindResource("DesignerItemStyle") as Style;
 myContentControl.Style = s;

如何将ACimage放入ContentControl

试试这个:

myContentControl.Content=ACimage;

最新更新