<UIElement>的制作方法在 WPF UI 中可交互或可点击 [英] How to make &lt;UIElement&gt; interactable or click-able in WPF UI

查看:77
本文介绍了<UIElement>的制作方法在 WPF UI 中可交互或可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

这是我的 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控件的任何/什么属性?

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?

推荐答案

首先提出您的直接问题:如何使 RichTextBox 的内容活跃".将 RichTextBox 上的 IsDocumentEnabled 属性设置为 True,如下所示:

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>

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

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>

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

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.

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

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>

这篇关于<UIElement>的制作方法在 WPF UI 中可交互或可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆