gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:33:46 25 4
gpt4 key购买 nike

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

我在互联网上搜索过,最接近的问题是关于如何使内部超链接可点击:The similar question: 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控件的任何/什么属性?

最佳答案

首先提出您的直接问题:如何使 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>

关于c# - 如何在 WPF UI 中使 <UIElement> 可交互或可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9073414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com