在后面的代码中将控件添加到textblock wpf中

时间:2016-02-01 04:04:53

标签: wpf wpf-controls

如何将此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>
&#13;
&#13;
&#13;

无论如何,我无法通过使用代码隐藏将项目控件添加到文本块控件或TextBlock控件中进入InlineUIContainer控件。

Thanhks。

结果如This

1 个答案:

答案 0 :(得分:1)

代码背后

        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>