textblox wpf 中的图像

image inside textblox wpf

你好,我想在文本框中显示图片,我可以用这段代码来做到这一点,但是正如你在图片中看到的那样,图片应该在左侧,文本应该在右侧,所以我该怎么做呢? see this image

  <Border Background="#f0f4f7">
            <StackPanel Background="#f5f6fa" Margin="10,10,10,370">
                <TextBlock Text="1397-1398"/>
                <Border VerticalAlignment="Center" Background="#edf0f5" BorderThickness="5">
                    <StackPanel Background="#ffffff" Height="30">
                        <StackPanel HorizontalAlignment="Right" Background="#ffffff" Margin="5"
                    Orientation="Horizontal">
                            <Image Source="E:\Aks\ICON\colorful-stickers-icons-set\pngx32\trash.png" 
                   Height="18"/>
                            <Image Source="E:\Aks\ICON\colorful-stickers-icons-set\pngx32\accept.png" 
                   Height="18"/>
                            <TextBlock Foreground="#7c7f84" Text="یکسری اطلاعات برای نمایش در اینجا"
                       Margin="3 0 0 0"/>
                        </StackPanel>

                    </StackPanel>
                </Border>
            </StackPanel>

        </Border>

使用带有两列的 Grid 将有效:

<Border Background="#f0f4f7">
    <StackPanel Background="#f5f6fa" Margin="10,10,10,0" VerticalAlignment="Top">
        <TextBlock Text="1397-1398"/>
        <Border Background="#edf0f5" BorderThickness="5">
            <Grid Background="#ffffff" Height="30">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <StackPanel Background="#ffffff" Margin="5" Orientation="Horizontal">
                    <Image Source="E:\Aks\ICON\colorful-stickers-icons-set\pngx32\trash.png" 
                           Height="18"/>
                    <Image Source="E:\Aks\ICON\colorful-stickers-icons-set\pngx32\accept.png" 
                           Height="18"/>
                </StackPanel>
                <TextBlock Foreground="#7c7f84" HorizontalAlignment="Right" Grid.Column="1" Text="یکسری اطلاعات برای نمایش در اینجا"
                   Margin="3 0 0 0"/>
            </Grid>
        </Border>
    </StackPanel>
</Border>

此外,在边框上使用 VerticalAlignment=Center 是不必要的,因为它包含在 StackPanel.

您可能还想查看 right-to-left 布局的文档: https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/bidirectional-features-in-wpf-overview