【问题标题】:How to "float" image in XAML (Windows 8 Metro)如何在 XAML 中“浮动”图像(Windows 8 Metro)
【发布时间】:2012-05-04 08:45:51
【问题描述】:

是否可以在 XAML 中为 CSS 中的图像执行类似 ffloat:left 的操作。我需要创建这样的东西:

具有可变的图像尺寸和文本长度。

编辑:在我的情况下,图像周围的文本扭曲不是静态的,它是在 C# 代码中创建的,返回一个 TextBlock 元素列表(带有运行)

【问题讨论】:

    标签: c# xaml windows-8 windows-runtime winrt-xaml


    【解决方案1】:

    使用 Silverlight 4,您可以使用 RichTextBox

     <RichTextBox TextWrapping="Wrap"  IsReadOnly="False">  
       <Paragraph>  
            More text here .. 
            <InlineUIContainer>  
                <Image Source="abc.jpg"/>  
            </InlineUIContainer>   
            more and more text here;  
            <LineBreak />  
        </Paragraph>  
    </RichTextBox> 
    

    看起来 Win8 Metro 有一个RichTextBox,也有一个InlineUIContainer,所以上面的方法应该可以工作!

    【讨论】:

    • 如何在 C# 中创建它?我有一个 TextBlocks 列表(带有运行),所以我想我可以将第一个转换为这个,我会好的
    【解决方案2】:

    解决方案似乎是使用本演示文稿中描述的 RichTextBlockOverflow 和 OverflowContentTarget:http://video.ch9.ms/build/2011/slides/APP-914T_Street.pptx

    【讨论】:

    • 看准了!另见this
    【解决方案3】:

    This question 似乎在要求与您想要的类似的东西。答案here 应该证明是您想要的解决方案。

    答案的摘要是,使用FlowDocument,如下例所示:

    <FlowDocument>
        <Paragraph>
            <Floater HorizontalAlignment="Left">
                <BlockUIContainer>
                    <Image Source="/FlowDocumentTest;component/dog.png" Width="100" /> 
                </BlockUIContainer>
            </Floater>
            Here is where the text goes to wrap around the image.
        </Paragraph>
    </FlowDocument>
    

    更新

    正如您的问题所述,您现在正在使用一些 C# 代码来生成 TextBlock/Run 元素,它们都可以是 Paragraph 对象的子级。因此,只需将您的 Paragraph 命名为:

    <FlowDocument>
        <Paragraph x:Name="textPara">
            <Floater HorizontalAlignment="Left">
                <BlockUIContainer>
                    <Image Source="/FlowDocumentTest;component/dog.png" Width="100" /> 
                </BlockUIContainer>
            </Floater>
        </Paragraph>
    </FlowDocument>
    

    然后在C#中,将你生成的TextBlocks 或Runs 添加到textPara 的Inlines 属性中,即

    var runToInsert = new Run("Some text to display");
    textPara.Inlines.InsertAfter(textPara.Inlines.FirstInline, runToInsert);
    

    【讨论】:

    • 我编辑了这个问题。在我的情况下,文本不是静态的,而是一堆文本框。我将它们插入 StackPanel,但如何将它们插入其中?
    • 用如何动态添加内容更新了我的答案。
    • 我不认为 Paragprah in a Paragraph 是有效的。编译器攻击
    • @IgorKulman 我的错误,我误读了 MSDN 文档,将用更好的示例进行更新
    • 顺便说一句,FlowDocument 必须放在 FlowDocumentPageViewer 中,它会创建我的场景中不需要的 UI 元素(如分页)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    相关资源
    最近更新 更多