I have a HyperLink control with text in its Text property.
With the following code:
var link = new HyperLink();
var img = new HtmlGenericControl("img");
img.Attributes.Add("src", "text.png");
link.Text = "Test";
link.Controls.Add(img);
When I do this, the image is rendered inside a a tag, but the text is not rendred.
Is there a way to render both the image and the text inside the Text property without throwing a third control in to the mix?
When you put any controls into the WebControl.Controls collection, it will ignore what you have inside Text. So if you want to render both text and other child controls, you should add the text into Controls:
var link = new HyperLink();
var img = new HtmlGenericControl("img");
img.Attributes.Add("src", "text.png");
link.Controls.Add(new Literal{ Text = "Test"}); // this line will add the text
link.Controls.Add(img);
I feel this should work out for you.
var link = new HyperLink();
var img = new HtmlGenericControl("img");
var lbl = new Label();
img.Attributes.Add("src", "text.png");
lbl.Text = "Test";
link.Controls.Add(img);
link.Controls.Add(lbl);
this.Controls.Add(link);
According to the MSDN article "The HyperLink control can be displayed as text or an image." So the answer is no, I'm afraid.
Related
I have been able to get links appearing in my RichTextbox. The first entry is correct but when I try appending a new line that also contains a link, the first entry is in the same position as the new link. When clicking on the link it retains it's first entries hyperlink.
I want each line to have it's own hyperlink (where it's underlined)
Code used to append a Link
public void AppendLink(string text, string linkText)
{
LinkLabel link = new LinkLabel();
link.Text = text;
link.LinkClicked += new LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
LinkLabel.Link data = new LinkLabel.Link();
data.LinkData = linkText;
link.Links.Add(data);
link.Location = this.logTextBox.GetPositionFromCharIndex(this.logTextBox.TextLength);
this.logTextBox.Controls.Add(link);
logTextBox.SelectionFont = UNDERLINE_FONT;
this.logTextBox.AppendText(s);
}
Called using this
AppendLogLine("Sealed ");
AppendLink(itemName, GetItemLink(itemName));
AppendLog(" is an unknown item. Keeping.");
Append Log and AppendLogLine does the same as AppendLink just doesn't create a link and uses a different Font
I'm constructing text, and some pieces of the text should contain a hyperlink. However, these hyperlinks do not redirect to a webpage but should open a page in the UWP app (the currently running UWP app, not a new instance of it or a different app).
A HyperlinkButton can only open URL's that lead to an external browser, it can't open a page inside the app.
Using an InlineUIContainer doesn't work, I get
Exception thrown: 'System.ArgumentException' in mscorlib.ni.dll
Additional information: Value does not fall within the expected range.
With this code
List<Inline> ic = new List<Inline>();
InlineUIContainer container = new InlineUIContainer();
TextBlock tbClickable = new TextBlock();
tbClickable.Foreground = new SolidColorBrush(Colors.Red);
tbClickable.Text = label?.name;
tbClickable.Tag = label?.id;
tbClickable.Tapped += TbArtist_Tapped;
container.Child = tbClickable;
ic.Add(container);
When I use
foreach (var item in ic)
{
dTb.Inlines.Add(item);
}
Where tbCurrent is the TextBlock.
Any other ways to get a clickable link as an Inline element?
Best case scenario I can attach a Tapped/Click event handler.
But opening the page via a URI method or so is also good.
I changed to a RichTextBlock and using Blocks I could add a clickable TextBlock.
This works in UWP.
List<Block> ic = new List<Block>();
Paragraph para = new Paragraph();
InlineUIContainer iuic = new InlineUIContainer();
TextBlock hpb = new TextBlock();
hpb.Text = "link text";
hpb.Tag = "some tag to pass on to the click handler";
hpb.Tapped += ClickHandler;
hpb.TextDecorations = TextDecorations.Underline;
hpb.Foreground = new SolidColorBrush((Windows.UI.Color)page.Resources["SystemAccentColor"]);
iuic.Child = hpb;
para.Inlines.Add(iuic);
ic.Add(para);
There is no reason you could not use a HyperlinkButton for this. Quick example:
<HyperlinkButton Click="HyperlinkButton_Click" Content="Click me" />
And the Click handler:
private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(MainPage));
}
I'm using GemBox.Document to generate an output document from a template. I want to insert an image inside a TextBox which will have the same size as that TextBox.
How can I do that?
DocumentModel document = DocumentModel.Load("mytemplate.dotx");
TextBox textBox = (TextBox)document.GetChildElements(true, ElementType.TextBox).First();
Picture picture = new Picture(document, "myimage.png");
textBox.Blocks.Add(new Paragraph(document, picture));
Try the following:
DocumentModel document = DocumentModel.Load("mytemplate.dotx");
TextBox textBox = (TextBox)document.GetChildElements(true, ElementType.TextBox).First();
// If needed you can adjust the TextBox element's inner margin to your requirement.
textBox.TextBoxFormat.InternalMargin = new Padding(0);
// If needed you can remove any existing content from TextBox element.
textBox.Blocks.Clear();
// Get TextBox element's size.
var textBoxSize = textBox.Layout.Size;
// Create and add Picture element.
textBox.Blocks.Add(
new Paragraph(document,
new Picture(document, "myimage.png", textBoxSize.Width, textBoxSize.Height)));
I hope this helps.
I am parsing HTML, generating a various blocks and adding them to RichTextBlock control. However, when I generate a Hyperlink from code behind I don't know how to remove underline property. I've seen posts about creating a HyperlinkButton and setting its Content property but it is definitely not something that I want because I can't make it to be in line with the rest of the text and it looks awful.
Below is the relevant code for generating a hyperlink:
private static Inline GenerateHyperLink(HtmlNode node)
{
Span s = new Span();
InlineUIContainer iui = new InlineUIContainer();
Hyperlink link = new Hyperlink();
link.Inlines.Add(new Run() { Text = node.InnerText });
link.NavigateUri = new Uri(node.Attributes["href"].Value, UriKind.Absolute);
s.Inlines.Add(link);
s.Inlines.Add(new Run() { Text = " " });
return s;
}
From MSDN:
A Hyperlink has a class inheritance that doesn't include
FrameworkElement, so it doesn't have a Style property. Nor does a
Hyperlink have a Template (it's not a true control).
Does it mean that there is no way to achieve that using Hyperlink element?
I want to be able to have some text clickable like in webpages in WPF. The control should have both non-functional text (for display) both also some of its parts as completely clickable.
Say like Wikipedia.
But this is an independent standalone offline app.
I tried various things but I couldn't do it, especially the clicking doesn't function like web pages, i.e. 1 click to open the url contained within the tools.
If you don't have a requirement that it be a full-blown FlowDocument, then you can just use a plain old WPF TextBlock, and put Hyperlinks in it.
<TextBlock>
Here's some text with a
<Hyperlink NavigateUri="Page2.xaml">link to another XAML page</Hyperlink>
and a
<Hyperlink NavigateUri="http://msdn.microsoft.com/">link to the
Web</Hyperlink>.
</TextBlock>
If you need scrolling, just put a ScrollViewer around it.
If you need the paginated, multi-column viewer, then you'll need to go with an all-out FlowDocument, but if all you want is text with hyperlinks, TextBlock + Hyperlink should be all you need.
you should try setting the flow document manually and creating hyperlinks within the flow document...
Here is some text taken from the following link:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/99ae9d9c-1dd4-4acd-8d5d-6eb739adeb98
"
Hi,
It is possible.
Here is a small example of creating hyperlink to paragraph/section/table.
In order to navigate to website, we can create a Frame Control for navigation. The hierarchical relationship of elements in this example is like this :
Frame-->FlowDocument-->Table-->Section-->Paragraph-->Hyperlink
In the code behind:
public Window1()
{
InitializeComponent();
// add a Frame for navigation
Frame frame = new Frame();
this.Content = frame;
//add FlowDocument
FlowDocument doc = new FlowDocument();
frame.Navigate(doc);
//add Table
Table table = new Table();
doc.Blocks.Add(table );
TableRowGroup group = new TableRowGroup();
table.RowGroups.Add(group );
TableColumn col1 = new TableColumn();
TableColumn col2 = new TableColumn();
table.Columns.Add(col1 );
table.Columns.Add(col2);
TableRow row1 = new TableRow();
TableCell cel1 = new TableCell();
row1.Cells.Add(cel1);
group.Rows.Add(row1);
//add Section
Section mySection = new Section();
//add section to the Table cell.
cel1.Blocks.Add(mySection);
Paragraph paraValue = new Paragraph();
Hyperlink hl = new Hyperlink(new Run("Click Here to Google"));
hl.Foreground = Brushes.Red;
paraValue.Inlines.Add(hl);
hl.FontSize = 11;
hl .NavigateUri =new Uri ("Http://www.google.cn");
mySection.Blocks.Add(paraValue);
}
If you have any additional question about this,please feel free to ask.
Thanks. "