在代码隐藏中将控件添加到文本块 wpf

Add controls into textblock wpf in code behind

如何将此 UI 转换为 wpf

背后的代码

<TextBlock x:Name="tblImgCorrectAnswer">
  <Span>Hello</Span>
  <Span Style="{DynamicResource FontMSMincho}">て</Span>
  <InlineUIContainer BaselineAlignment="Center">
    <TextBlock>
      <Image Source="Images/icon.ico" Width="40" Height="40"/>
    </TextBlock>
  </InlineUIContainer>

</TextBlock>

无论如何,我无法使用代码隐藏将项目控件添加到文本块控件或将 TextBlock 控件添加到内联UI容器控件。

谢谢。

结果像This

代码隐藏

        var textBlock = new TextBlock();
        var inlineHello = new Span();
        inlineHello.Inlines.Add("Hello");
        var inlineJSighn = new Span();
        inlineJSighn.Inlines.Add(" JSighn");
        BitmapImage logo = new BitmapImage();
        logo.BeginInit();
        logo.UriSource = new Uri("pack://application:,,,/Images/Koala.jpg", UriKind.Absolute);
        logo.EndInit();
        var inlineUiContainer = new InlineUIContainer(new Image { Source = logo });
        inlineUiContainer.BaselineAlignment = BaselineAlignment.Center;
        textBlock.Inlines.Add(inlineHello);
        textBlock.Inlines.Add(inlineJSighn);
        textBlock.Inlines.Add(inlineUiContainer);


        LayoutRoot.Children.Add(textBlock);

Xaml

<Window x:Class="AddChildrenToGrinInCode.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid x:Name="LayoutRoot"/></Window>