How to Add Text to a Wpf Label in Code

How to add text to a WPF Label in code?

Try DesrLabel.Content. Its the WPF way.

Adding a TextBlock to a Label in C# Code WPF

Just set the TextBlock as the content of the label to achieve your requirement.

Label lbl = new Label ();
TextBlock txtBlock = new TextBlock ();
txtBlock.TextWrapping = TextWrapping.Wrap;
lbl.Content = txtBlock;

Change label text at runtime in wpf C#

If you just want to display a label with a incrementing number you can create a Task and then use a delay (Thread.Sleep()) to give the UI time to refresh the label.

Because you cannot change UI Elements within a separate Thread, you have to update the UI with the UI Dispatcher.

Sample Code

var length = 1000;

Task.Run(() =>
{
for (int i = 0; i <= length; i++)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() => {
lblMsg.Content = "Test" + i;
}), DispatcherPriority.Render);
Thread.Sleep(100);
}
});

How can I wrap text in a label using WPF?

The Label control doesn't directly support text wrapping in WPF. You should use a TextBlock instead. (Of course, you can place the TextBlock inside of a Label control, if you wish.)

Sample code:

<TextBlock TextWrapping="WrapWithOverflow">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec adipiscing
nulla quis libero egestas lobortis. Duis blandit imperdiet ornare. Nulla
ac arcu ut purus placerat congue. Integer pretium fermentum gravida.
</TextBlock>

how to add text in rectangle with code behind wpf

well rectangle is not a content control it is derived from shape object... we cannot use it as a panel.

instead of rectangle you can use a border..

if your requirement demands a Rectangle then what you can do is... create a Grid> then add rectangle to that Grid> and create a textblock and add it to same grid... since grid is not physically visible it appears like text added to rectangle ..

i will try to post a sample

Edit:
following code will help you to understand it better

for (int i = 0; i < _RoomX.Count; i++)
{
_RoomX[i] = (Convert.ToDouble(_RoomX[i]) * 20).ToString();
_RoomY[i] = (Convert.ToDouble(_RoomY[i]) * 20).ToString();

var rectangle = new Rectangle()
{

Stroke = Brushes.Black,
Fill = brush,
Width = Convert.ToDouble(_RoomX[i]),
Height = Convert.ToDouble(_RoomY[i]),
Margin = new Thickness(
left: 15,
top: 50,
right: 0,
bottom: 0),
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top

};

Grid grid = new Grid();
grid.Children.Add(rectangle);
TextBlock textblock = new TextBlock();
textblock.Text = "Text to add";
grid.Children.Add(textblock);

mainCanvas.Children.Add(grid);
}

How to center a label text in WPF?

use the HorizontalContentAlignment property.

Sample

<Label HorizontalContentAlignment="Center"/>

How to add a line break in a label

I don't think that Environment.NewLine is not working in your case. Check the height of your label in XAML. When you add a Line Break then it increases the content in label and if height is not enough then you cant see that. I think you are facing this problem other wise i don't see any problem with Environment.NewLine :) Try adding height and tell me

Draw diagonal Text/TextBlock/Label/Control in WPF relative to its container

You have to compute the angle of the TextBlock whenever the Grid's size changes, e.g. using a converter.

Here is an example:

<Window x:Class="WpfApplication13.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication13"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:AngleConverter x:Key="AngleConverter"/>
</Window.Resources>
<Grid x:Name="grid">
<TextBlock Text="Draft" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="120" FontWeight="Bold" Foreground="#FFA43A3A"
RenderTransformOrigin="0.5,0.5">
<TextBlock.RenderTransform>
<MultiBinding Converter="{StaticResource AngleConverter}">
<MultiBinding.Bindings>
<Binding ElementName="grid" Path="ActualWidth"/>
<Binding ElementName="grid" Path="ActualHeight"/>
</MultiBinding.Bindings>
</MultiBinding>
</TextBlock.RenderTransform>
</TextBlock>
<Path Stroke="Red" StrokeThickness="2" Stretch="Fill">
<Path.Data>
<LineGeometry StartPoint="1,0" EndPoint="0,1" />
</Path.Data>
</Path>
</Grid>
</Window>

The converter code:

using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace WpfApplication13
{
public class AngleConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
double width = values[0] as double? ?? 0;
double height = values[1] as double? ?? 0;

double angleRadians = Math.Atan2(height, width);
return new RotateTransform {Angle = - angleRadians * 180.0 / Math.PI};
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

Result:

Screenshot 1

Screenshot 2



Related Topics



Leave a reply



Submit