如何使在 WPF UI 中可交互或可点击

发布于 2025-01-01 01:39:12 字数 1233 浏览 0 评论 0原文

这是我使用 WPF 设计 UI 的第一天。我查阅了Flow Document的MSDN官方文档,发现可以将UI控件放置在RichTextBox中。我确实放了一个按钮,但发现它不可交互 - 我无法单击它,因为它是灰色的。我还尝试了其他控件,它们都显示良好,但只是不支持交互。即使是超链接也不起作用。

我在互联网上搜索过,最接近的问题是关于如何使内部超链接可点击: 类似的问题:C# WPF Text with links

我做了同样的事情,但没有成功!所有组件都显示良好,但无法单击。

这是我的 XAML 代码:

        <RichTextBox Grid.Row="1" Margin="14.007,31.067,22.011,46.305" Name="rtxtRslt" BorderBrush="White" >
        <FlowDocument>
            <Section FontSize="15">
                <Paragraph>
                    <Bold>Click on this:</Bold>
                    <Italic><Hyperlink  NavigateUri="http://stackoverflow.com">http://www.jxitc.info</Hyperlink></Italic>

                </Paragraph>

                <BlockUIContainer>
                    <Button Click="Button_Click">Also Click On This</Button>
                </BlockUIContainer>
            </Section>
        </FlowDocument>
    </RichTextBox>

任何人都可以给我一些建议: 1.是否可以使其可点击 2.如果是的话,我是否忘记设置RichTextBox控件的任何/什么属性?

This is my first day to design UI using WPF. I have looked up MSDN official document of Flow Document and found that I can place an UI control inside a RichTextBox. I did put a button in but found it's not interactable - I cannot click on it as it's grey. And I also tried other controls and they all displayed fine but just don't support interaction. Even a hyperlink doesn't work.

I have searched over internet, the closest question ever asked is about how to make an inside hyperlink click-able: The similar question: C# WPF Text with links

I did the same thing but it didn't work! All component displayed well but just are not able to be clicked.

Here is my XAML code:

        <RichTextBox Grid.Row="1" Margin="14.007,31.067,22.011,46.305" Name="rtxtRslt" BorderBrush="White" >
        <FlowDocument>
            <Section FontSize="15">
                <Paragraph>
                    <Bold>Click on this:</Bold>
                    <Italic><Hyperlink  NavigateUri="http://stackoverflow.com">http://www.jxitc.info</Hyperlink></Italic>

                </Paragraph>

                <BlockUIContainer>
                    <Button Click="Button_Click">Also Click On This</Button>
                </BlockUIContainer>
            </Section>
        </FlowDocument>
    </RichTextBox>

Can anyone give me some suggestion:
1. is it possible to make it click-able
2. if yes, if I forgot to set any/what attribution of the RichTextBox control?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情场扛把子 2025-01-08 01:39:12

首先你的直接问题:如何使 RichTextBox 的内容“活动”。将 RichTextBox 上的 IsDocumentEnabled 属性设置为 True,如下所示:

   <RichTextBox Grid.Row="1" Margin="14.007,31.067,22.011,46.305" Name="rtxtRslt" BorderBrush="White" 
                    IsDocumentEnabled="True">
        <FlowDocument>
            <Section FontSize="15">
                <Paragraph>
                    <Bold>Click on this:</Bold>
                    <Italic>
                        <Hyperlink  NavigateUri="http://stackoverflow.com">http://www.jxitc.info</Hyperlink>
                    </Italic>

                </Paragraph>

                <BlockUIContainer>
                    <Button Click="Button_Click" >Also Click On This</Button>
                </BlockUIContainer>
            </Section>
        </FlowDocument>
    </RichTextBox>

现在问一个不言而喻的问题:您是否必须位于 RichTextBox 中?事实上,RichTextBox 上有一个特殊属性可以使嵌入的 UI 元素处于活动状态,这表明这不是该控件的正常用法。它旨在托管可编辑的 FlowDocument 内容。因此,RichTextBox 的用户通常会创建包含文档使用者可以单击的按钮的文档,如果这有助于明确区分,我不这么认为知道。
然而,话虽如此,默认情况下,托管在简单 FlowDocumentPageViewer 中的 FlowDocument 处于活动状态。

 <FlowDocumentPageViewer>
        <FlowDocument>
            <Section FontSize="15">
                <Paragraph>
                    <Bold>Click on this:</Bold>
                    <Italic>
                        <Hyperlink NavigateUri="http://stackoverflow.com">http://www.jxitc.info</Hyperlink>
                    </Italic>

                </Paragraph>

                <BlockUIContainer>
                    <Button Click="Button_Click" >Also Click On This</Button>
                </BlockUIContainer>
            </Section>
        </FlowDocument>

    </FlowDocumentPageViewer>

现在到另一个未说出口的问题(难以言表?)您是否必须处于 FlowDocument 内容中? FlowDocument 内容与 UIElement 类似,但并非派生自 UIElement。因此,UIElements 的许多开箱即用功能不可用。如果您需要 UI 中的文档功能,FlowDocuments 可以提供一个很好的开始,但它们本身也带来了相当大的学习曲线。

你的问题的标题,如果从字面上看,让我觉得你可能只想要一个 WPF UI,允许你嵌入按钮和超链接并让它们工作(喘息)。这当然是默认行为。如果您不需要 FlowDocument 提供的文档外观和感觉,也不需要 RichTextBox 提供的实时文档编辑,您可能会考虑更“传统”的 WPF 布局。

 <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch"
                Margin="20">
        <TextBlock>
                    <Bold>Click on this:</Bold>
                    <Italic>
                        <Hyperlink NavigateUri="http://stackoverflow.com">http://www.jxitc.info</Hyperlink>
                    </Italic>
        </TextBlock>
        <Button Click="Button_Click" 
                Margin="0,20,0,0">Also Click On This</Button>
    </StackPanel>

First off your direct question: how to make the content of the RichTextBox "active". Set the IsDocumentEnabled property to True on the RichTextBox like shown here:

   <RichTextBox Grid.Row="1" Margin="14.007,31.067,22.011,46.305" Name="rtxtRslt" BorderBrush="White" 
                    IsDocumentEnabled="True">
        <FlowDocument>
            <Section FontSize="15">
                <Paragraph>
                    <Bold>Click on this:</Bold>
                    <Italic>
                        <Hyperlink  NavigateUri="http://stackoverflow.com">http://www.jxitc.info</Hyperlink>
                    </Italic>

                </Paragraph>

                <BlockUIContainer>
                    <Button Click="Button_Click" >Also Click On This</Button>
                </BlockUIContainer>
            </Section>
        </FlowDocument>
    </RichTextBox>

Now to the unspoken question: do you have to be in a RichTextBox at all? The fact that there is a special property on the RichTextBox to make embedded UI elements active kinda indicates that is not the normal usage for this control. It is meant to host editable FlowDocument content. So the user of the RichTextBox would typically be creating the document that hosts the button that a consumer of the document could click, if that helps make the distinction clear I don't know.
However, all that being said, your FlowDocument hosted instead in a simple FlowDocumentPageViewer is active by default.

 <FlowDocumentPageViewer>
        <FlowDocument>
            <Section FontSize="15">
                <Paragraph>
                    <Bold>Click on this:</Bold>
                    <Italic>
                        <Hyperlink NavigateUri="http://stackoverflow.com">http://www.jxitc.info</Hyperlink>
                    </Italic>

                </Paragraph>

                <BlockUIContainer>
                    <Button Click="Button_Click" >Also Click On This</Button>
                </BlockUIContainer>
            </Section>
        </FlowDocument>

    </FlowDocumentPageViewer>

Now to the other unspoken question (unspeakable?) do you have to be in FlowDocument content at all? FlowDocument content is similar to, but not derived from UIElement. As such, many of the out-of-the-box features of UIElements are not available. If you need document functionality in the UI FlowDocuments can provide a great start but bring with them a pretty big learning curve in their own right.

The title of your question, if taken literally, makes me think you might just want a WPF UI that allows you to embed Buttons and Hyperlinks and have them work (gasp). That is certainly the default behavior. If you do not need the document look and feel that FlowDocument provides nor the real time document editing that RichTextBox provides you might consider a more "traditional" WPF layout.

 <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch"
                Margin="20">
        <TextBlock>
                    <Bold>Click on this:</Bold>
                    <Italic>
                        <Hyperlink NavigateUri="http://stackoverflow.com">http://www.jxitc.info</Hyperlink>
                    </Italic>
        </TextBlock>
        <Button Click="Button_Click" 
                Margin="0,20,0,0">Also Click On This</Button>
    </StackPanel>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文