How to programatically add an image to a textblock? - c#

I'm trying to set an icon to be attached to a textblock when it is saved but I'm not sure how I would do this programatically.What I'm thinking is you could set the value to the left of the textbox to an image.I have tried this but I'm not sure how I would set it to an image icon:
NoteGridView.SetValue(Canvas.LeftProperty(double)block.GetValue(Canvas.LeftProperty));
This is how I'm adding the notes to the layout:
// Creates a new textblock that for this note.
TextBlock block = new TextBlock();
//block.SetValue
notepadTxtBox.SetValue(Canvas.LeftProperty, (double)block.GetValue(Canvas.LeftProperty));
block.Width = 250;
block.Height = 125;
block.Text = notepadTxtBox.Text;
// Add that note to the grid.
NoteGridView.Items.Add(block);
Does anyone have any idea how I would acheive this or is it possible to do in the xaml code?
This might give a better understanding of what I'm trying to do:

You could put a StackPanel inside of the TextBlock, containing an Image, and another TextBlock, like so:
<TextBlock>
<StackPanel Orientation="Horizontal">
<Image Name="YourImage" Source="image/Under-Construction.png" />
<TextBlock Text="Your Text Block Text" />
</StackPanel>
</TextBlock>
You would then be able to set the Image programmatically like so:
YourImage.Source = "path.to.image.file"

Related

How to add tool tip to "inline" hyperlink

I have a TextBlock control in my UWP app and inside it I have several text and hyperlinks. I shorten the urls of the hyperlinks when showing to user but I want to add a tooltip that shows the full url when user hovers over hyperlink.
I know how to add tool tips to controls but in this case Hyperlink object is not a control (Windows.UI.Xaml.Documents.Hyperlink). Although the code below does not give any errors, it does not work either, it does not show the tool tip:
var textBlock = new TextBlock();
var link = new Hyperlink();
var run = new Run();
run.Text = "http://www.twitter.com/.../";
ToolTip toolTip = new ToolTip();
toolTip.Content = "http://www.twitter.com/blablabla/longurl/";
ToolTipService.SetToolTip(link, toolTip);
link.Inlines.Add(run);
textBlock.Inlines.Add(link);
Is there a way to add tool tips to Inline objects inside a TextBlock?
As you known, Hyperlink object is not a control, it seems like ToolTipService.ToolTip attached property take no effect on it. As a workaround I recommend you to use RichTextBlock instead of TextBlock. This is because RichTextBlock supports InlineUIContainer class at the same time TextBlock doesn't support. HyperlinkButton can be added to InlineUIContainer, since ToolTipService.ToolTip take effects on HyperlinkButton, so we can use a RichTextBlock with HyperlinkButton as follows to meet your requirements:
<RichTextBlock>
<Paragraph>
<Italic>This is an inline bing: </Italic>
<InlineUIContainer>
<HyperlinkButton Content="bing" NavigateUri="http://www.bing.com" VerticalAlignment="Bottom" Padding="0,-3" xml:space="preserve">
<ToolTipService.ToolTip>
<ToolTip
Content="Offset ToolTip."
HorizontalOffset="20"
VerticalOffset="30" />
</ToolTipService.ToolTip>
</HyperlinkButton>
</InlineUIContainer>
Mauris auctor tincidunt auctor.
</Paragraph>
</RichTextBlock>
You can do this with a HyperlinkButton:
<HyperlinkButton NavigateUri="http://www.mrlacey.com/" >
<TextBlock Text="mrlacey.com">
<ToolTipService.ToolTip>
<TextBlock Text="http://mrlacey.com/" />
</ToolTipService.ToolTip>
</TextBlock>
</HyperlinkButton>

Change text of textblock within a panoramaitem

I need to use two textblock within a single panorama item. I have used a grid to implement this.
Problem is that I need to change the text of the textblock programmatically.
<phone:PanoramaItem Header="Current Status">
<Grid >
<TextBlock x:Name="Spent" Margin="138,0,90,464" FontSize="40"/>
<TextBlock x:Name="Left" Margin="138,50,90,414" FontSize="40"/>
</Grid>
</phone:PanoramaItem>
Saying:
Spent.Text = Convert.ToString(total_spent);
Left.Text = Convert.ToString(total_left);
in the c# file gives an error.
Please tell me how to change the text of the individual textblocks :)
P.S:
I am an absolute beginner and almost completely self taught, so a simple answer will be useful
Thanks
You need to use Text property of the TextBlock to get/set the Text.
From MSDN : TextBlock.Text
Gets or sets the text contents of a TextBlock.
Try This:
Spent.Text = Convert.ToString(total_spent.Text);
Left.Text = Convert.ToString(total_left.Text);

Add textfield programmatically

Is it possible to add a textfield programmatically in WP8?
I know in Java I can use something like this:
canvas.drawText(Days, TypedValue + width, labelsY, paint);
It uses the:
private Paint paint = new Paint();
Is something like this possible in WP8?
I have to use this because it the number of textboxes needed varies in the application.
I know I can use some textboxes, and just set the .visible state. But I would like to learn to do this programmatically.
Thanks
Of course, everything which you do in XAML can be done programmatically. Say you have the following:
<Grid x:Name="myGrid">
<TextBox Text="Initial text" />
</Grid>
In the code-behind xaml.cs for that same XAML file, you can do the same by adding to the Grid's Children collection (a field is created for the named element):
TextBox t = new TextBox();
t.Text = "Initial text"
myGrid.Children.Add(t);
or more succinctly
myGrid.Children.Add(new TextBox { Text = "Initial text" } );

How to put textblock inside scrollviewer by coding behide ? (wpf)

I have a xaml code like this
<Grid>
<... some grid row and column definitions .../>
<ScrollViewer Grid.Column="1" VerticalScrollBarVisibility="Auto">
<TextBlock "some attribute" />
</ScrollViewer>
</Grid>
I don't know how to tie Textblock with Scrollviewer in C#. I want to use Textblock with Scrollviewer.
If you have another idea, please tell me.
Many Thanks for your help. :D
just use the Content Property
var myScrollViewer = new ScrollViewer();
myScrollViewer.Content= new TextBlock();
Edit
or in combination with XAML
XAML
<ScrollViewer Name="myScrollViewer " Grid.Column="1" VerticalScrollBarVisibility="Auto"/>
Code behind
myScrollViewer.Content= new TextBlock(); // or what ever you want to add :-)

WPF Label Content: How to avoid reference to Alt Key?

I have defined the WPF label with content="Label_Label".
While displaying it shows "LabelLabel". The first "_" is
considered for "Alt Key" Reference.
In my real requirement I am assigning Content to Label
dynamically, So please specify solution to this problem.
<Label Content="Label_Label" Height="28" HorizontalAlignment="Left" Margin="73,42,0,0" Name="label1" VerticalAlignment="Top" Width="88" UseLayoutRounding="False" ClipToBounds="False" />
If you're binding your label's content to some data and can't "escape" the underscore in the data (per mwtb's answer), then the other option is to wrap the text in a TextBlock inside the label. TextBlocks have no concept of an access key so they'll display the text as is.
So instead of this:
<Label Content="{Binding MyText}" />
You can do this:
<Label><TextBlock Text="{Binding MyText}" /></Label>
Assuming "MyText" contains the string "Hello_World", the former will display HelloWorld while the latter will display Hello_World.
Update
Per your comment, here's the same thing in code:
var tb = new TextBlock();
tb.SetBinding(TextBlock.TextProperty, new Binding("MyText"));
var label = new Label
{
Content = tb
};
That's untested but should work. Obviously you'd then have to add "label" to your visual tree in the usual manner.
You can escape the underscore by using two in a row:
Content="Label__Label"
I'm not sure what additional question you're implying by "In my real requirement I am assigning Content to Label dynamically"
Honestly, the only difference between a Label and a ContentControl is that a Label allows use of an access key. If you don't want the access key feature, just use a ContentControl.

Categories

Resources