Can i add a string including baselinealignments programmatically to a textblock? - c#

I'm working with VS2015 and WPF on a WPF-app with dynamic forms.
On my WPF windows i got some textblocks to which i want to add at runtime programmatically and dynamically a text (is it really the text?) like "51x4 + 4" per example.
When i try to set the string in the text-property at runtime i can only see the given string.
But when i add it in the content of the textblock in XAML i can see at runtime that the first "4" is subscripted.
Here the hardcoded example:
<TextBlock Name="textBlock1" Height="50">
5x<Run BaselineAlignment="Subscript">4</Run> + 4
</TextBlock>
Thanks in advance!

You add Run elements to the Inlines property of the TextBlock:
txt.Inlines.Add(new Run("5x"));
txt.Inlines.Add(new Run("4") { BaselineAlignment = BaselineAlignment.Superscript });
txt.Inlines.Add(new Run(" + 4"));

Related

Set Text attribute of multiple TextBlocks

Is it possible to set the Text attribute of multiple TextBlocks without calling each one separately? The possibility to iterate through them?
Like in the following example:
<TextBlock x:Name="textblock_a" Text="Original text"/>
<TextBlock x:Name="textblock_b" Text="Original text"/>
To
<TextBlock x:Name="textblock_a" Text="Modified text"/>
<TextBlock x:Name="textblock_b" Text="Modified text"/>
Probably the easiest way:
foreach(var item in new[] {textblock_a, textblock_b})
item.Text = "Modified text";
P.S.: I wouldn't use word attribute without mentioning xaml, Text is a property.
The WPF way of doing this is by using Binding.
As state HERE(easy example) you can bind the Text value of your TextBlocks to the same property.
Do not forget the INotifyPropertyChanged so everything updates when the string changes.

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" } );

Custom fontsize for each pivotiem of a pivot

In my project of a Windows Phone 7 app, I use some Pivot, and I would like to know if it is possible to change the fontsize of the header of a pivotitem in c#.
For the moment, I know how to change all of the fontsize of the pivot. Here how I do it <controls:Pivot x:Name="EventPivot">
<controls:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock x:Name="header" Text="Salut" FontSize="30" />
</DataTemplate>
</controls:Pivot.HeaderTemplate>
</controls:Pivot>
But my problem is that I didn't find how to change the Header text in c#. I tried
TextBlock headerText = new TextBlock { Text = "toto", FontSize= 20, TextWrapping = TextWrapping.Wrap};
var myPivotItem = new PivotItem { Header = headerText};
myPivotItem.Content = myScrollViewer;
EventPivot.Items.Add(myPivotItem);
But that didn't work.
Maybe if I use some sort of DataTemplate as I did for the Pivot I could solve my problem but I don't know how to implement it in c#...
I wonder also if the height of the PivotItem could be independant from all other PivotItems. Because for the moment, the height of each header is based by the larger height of the others PivotItems.
Thanks for all of your answers.
Bye
Drhouse87
Before implementing such a thing I'd test very carefully what the users think of such varying header item sizes. Not also that you'll likely have spacing issues as a result of this as the space reserved for the header will be applied to all pivot items.
That said, it should be possible to do this by binding a value to use for the FontSize and then applying a converter if need be.
You can't refer to items within a template by name from the code behind of the page as there could be multiple instances of the control wiht that name.

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