How to make <UIElement> interactive or click in WPF interface
This is my first day to develop a user interface using WPF. I looked at the official MSDN Flow Document and found that I could place the user interface control inside the RichTextBox . I pressed the button, but found that it is not interacting - I can not click on it because it is gray. And I also tried other controls, and they all looked great, but just didn't support the interaction. Even the hyperlink is not working.
I searched over the Internet, the closest question I have ever asked is how to make an internal hyperlink pressed: Similar question: C # WPF Text with links
I did the same, but it didn’t work! All components are displayed well, but simply can not be pressed.
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 someone give me some suggestion: 1. is it possible to make it clickable 2. If so, if I forgot to set any attributes of the RichTextBox control?
First of all, right to your question: how to make RichTextBox content “active”. Set the IsDocumentEnabled property to True on the RichTextBox , as 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: should you even be in a RichTextBox? The fact that RichTextBox has a special property for creating active user interface elements indicates that this is not normal for this control. It is designed to host editable FlowDocument content. Thus, a RichTextBox user usually creates a document on which a button is placed that the consumer of the document can click if it helps to make the difference understandable, I don’t know. However, all that said, your FlowDocument, placed 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 another unspoken question (unspeakable?), Should you even be in the contents of a FlowDocument? The contents of the FlowDocument are similar but not derived from the UIElement. Thus, many of the predefined UIElements functions are not available. If you need document functions in the user interface, FlowDocuments can provide an excellent start, but bring with it a fairly large learning curve in its own right.
The name of your question, if taken literally, makes me think that you just need a WPF interface that allows you to insert buttons and hyperlinks and make them work (suffocate). This is certainly the default behavior. If you don't want the look of the document that FlowDocument provides or the real-time editing of the document that RichTextBox provides, you can 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>