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.
Related
I wish to add a Label in WPF that displays string from two different DynamicResources.
I want each DynamicResource to be on a new line.
My existing code is:
<Label x:Name="MyTextDisplay"
Grid.Row="3"
Grid.ColumnSpan="2"
Background="Red"
BorderBrush="Blue"
BorderThickness="1"
Margin="2, 2, 2, 2">
<TextBlock TextWrapping="Wrap" Text="{DynamicResource MyTextLine1}" Grid.ColumnSpan="2"/>
</Label>
I have another DynamicResource called MyTextLine2 that I want to display below MyTextLine1 but in the same Label.
How can I do this?
I have looked at these examples here but they dont display on new lines: How to bind multiple values to a single WPF TextBlock?
I faced the same problem and finally I found a solution.
Just use \r\n linebreak instead of just \n.
So, your resourse must look like:
<system:String x:Key="MyText" xml:space="preserve">Line 1
Line 2</system:String>
I realy don't know why this notation must be used only for dynamic resources, but it works for me
I need to create a XAML textbox immediately after another textbox using code-behind. Maybe something like this:
Before
<StackPanel>
<TextBox Name="TextBox1"/>
<TextBox Name="TextBox2"/>
<TextBox Name="TextBox3"/>
</StackPanel>
After
<StackPanel>
<TextBox Name="TextBox1"/>
<TextBox Name="TextBox2"/>
<TextBox Name="InsertedTextBox"/> <!--This textbox was inserted after 'TextBox2'-->
<TextBox Name="TextBox3"/>
</StackPanel>
I have the name of the textbox I wish to insert the other textbox after. May I know how I can insert a textbox after another textbox which I know the name of?
Note: I am programming for Universal Windows.
Thanks in advance.
You need to name the StackPanel to reference it in code, and you need the index of the preceding TextBox:
var index = this.stackPanel1.Children.IndexOf(TextBox2);
this.stackPanel1.Children.Insert(index + 1, new TextBox { Name = "InsertedTextBox" });
You could try this, note I've given the StackPanel a name.
// Where 2 is the index.
this.StackPanel1.Children.Insert(2, new TextBox());
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.
While creating GUI using xaml, I created a textbox with a tag like this:
<TextBox Name="TextBox" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="216,178,143,120" Width="158"
Tag="myTag"/>
Now I want to let the user be able to change this tag. For that, I am looking for a kind of function of form:
TextBox.SetTag( "User Provided Tag" )
So that the tag can be changed into this one:
<TextBox Name="TextBox" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="216,178,143,120" Width="158"
Tag="User Provided Tag"/>
After searching the internet for quite a while, I didn't come up with any practical solution. Could someone help? Thanks.
You can use a binding between two controls. Say a user is allowed to enter a tag value from a TextBox. You just bind the Tag of the second TextBox to a Text property of the first TextBox:
<TextBox Name="enterTagTextBox" />
<TextBox Name="getTagTextBox" Tag="{Binding ElementName=enterTagTextBox, Path=Text}"/>
To test it I added a Button in my XAML:
<Button Height="25" Click="Button_Click_1"/>
In code behind I just retrieve the tag value and display it like this:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string text = this.getTagTextBox.Tag.ToString();
global::System.Windows.Forms.MessageBox.Show(text);
}
I think your problem may arise from you naming your TextBox, "TextBox". Try giving it a name that does not clash with the class name, like "txtMyTextBox".
Then you can do, txtMyTextBox.Tag = "User Provided Tag";.
Or you can bind to it as PiotrWolkowski suggests.
However, I would like to add, that it seems like there should be a cleaner way of achieving your desired behaviour. With the caveat that I don't know the details of what you are trying to implement.
I strongly suggest creating a ViewModel (see: MVVM Pattern) to hold the data you want users to be able to edit. Then use the bindings in WPF to display the data.
You would need to use the property element usage in order to set the Tag property in Extensible Application Markup Language (XAML) to anything other than an object with a known and built-in type converter, such as a string.
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);