Add textfield programmatically - c#

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

Related

Can i add a string including baselinealignments programmatically to a textblock?

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

Get content of specific Table cell and change text color

I want to check if the content of a table cell starts with + or - and therefore make the text color green or red. How can I do that in code-behind?
Result should look like this:
A very generic example for a single TextBox, with belonging Code-Behind (Not using MVVM):
XAML:
<TextBox x:Name="MyTextBox" Text="+0.25$" />
Code-Behind:
if (MyTextBox.Text.StartsWith("-"))
MyTextBox.Foreground = Brushes.Red;
else
MyTextBox.Foreground = Brushes.Green;

How to programatically add an image to a textblock?

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"

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.

How to bold & add "*" to TextBlock when Tab Can be saved

I have tabs representing documents, something like in Word. My TabControl is bound to an ObservableCollection<TabViewModel>. TabViewModel has a property CanSave indicating whether a document can be save. When it can be saved, I want to bold it and prefix it with an "*". How can I do this? I think I need to 1st make CanSave a DependencyProperty. And add a trigger. But what about prefix the "*"?
You don't need to make a DependencyProperty; you just need to implement INotifyPropertyChanged.
You can bind the property to the Visibility of a separate <TextBlock>*</TextBlock> and to the weight of the title using triggers.
A simple (maybe ugly, but should definitely work):
void CanSave(....)
{
bool canSave = GetValueBlahBlah();
if (tb.IsVisible != canSave)
tb.Visibility = canSave ? Visibility.Visible : Visibility.Collapsed;
}
tb represents the TextBlock you wanna show and hide according to the CanSave state.
You might also wanna create a DependencyProperty as you said and set the TextBlock s (you will have to use a separate TextBlock for the star - or use Runs which are bindable in WPF 4+) Visibility/FontWeight according to it via DataTriggers.
You could also set the titles of your tabs via binding....
<TabControl >
<TabItem >
<TabItem.Header>
<TextBlock Text="{Binding TabTitle1}" />
<TabItem.Header>
</TabControl>
and then set the title on your data model
Tab1Title="* " + "some nice tab title";
you could also use binding to set the font to bold....
FontWeight="{Binding Tab1FontWeight}"

Categories

Resources