Adding a TextBlock to a Label in C# Code WPF - c#

In my code I am creating a new Label and formatting it and making it a child of a Stacked Panel. But inside of the Label I need to add a Text Block and I am having trouble to find out how to do this.
Using Code only I need to the WPF created by the code to work like this:
<Label Background="#000000" Foreground="#FFFFFF">
<TextBlock TextWrapping="Wrap" Text="Text Here"/>
</Label>
But I can not figure out how to get that TextBlock inside of the Label in the code, what I thought would work isn't working because it is a Label:
NewLabel.Children.Add(NewTextBlock);
But this works when I use it to add the NewLabel to the StackedPanel.
The whole reason I need this to work is because I need text wrapping in the Label, but can't use just the TextBlock or other Control.

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;

Also you could add a style for "Label" to your Resources which contains a default DataTemplate for type string. So all string content in any Label is wrapped.
<Application.Resources>
<Style TargetType="Label">
<Style.Resources>
<DataTemplate xmlns:sys="clr-namespace:System;assembly=mscorlib" DataType="{x:Type sys:String}">
<TextBlock TextWrapping="Wrap" Text="{Binding}" />
</DataTemplate>
</Style.Resources>
</Style>
</Application.Resources>
Then all you Need to do is set the string Content.
<Label Content="A very long string for my Label" />
or
var Label = new Label { Content = "A very long string for my Label" }

Related

Adding a new component in c# WPF

I am new to C# and WPF. I wanna add the following element in the page.
<Label HorizontalAlignment="Left" Width="99" MouseMove="Variable_MouseMove" x:Name="VariableLabel">
<Run
Background="red"
Text="Variable"
x:Name="Variable"
/>
</Label>
I wanna add it dynamicly to the Canvas, like that:
Label label = new Label();
Run r = new Run();
label.Children.add(r); // error: label doesn't have the definition of Children
canvas.Children.add(label);
I want to set the background color behind only the text, rather than the whole label, so I use Run as Label child element and set the child element's background.
What should I do? Thanks!
The label don't have Children but in you case you can use Content. So
label.Content = run;
will work

Explore a WPF TabItem

I want to get the content of a graphical elements present in a WPF TabItem.
Consider the following code for the window :
<TabItem x:Name="TheItem" Header="Form">
<Grid x:Name="MainGrid">
<TextBox x:Name="txtContent" Text="Hello I'm some content !" />
<TextBox x:Name="txtOther" Text="Some other content" />
</Grid>
</TabItem>
I've looked up the TabItem documentation on MSDN but failed to find any useful information.
Is there any way to get the content from the "txtContent" textbox from a TabItem ?
Try this:
Grid grid = TheItem.Content as Grid;
TextBox txtContent = grid.Children[0] as TextBox;
string text = txtContent.Text;
Or simply:
TextBox txtContent = MainGrid.Children[0] as TextBox;

how to set text in back end for inside controls in pivot uwp

I am trying to set the text for the controls inside the pivot item in the following way.
<Pivot x:Name="galleryPivot"
HeaderTemplate="{StaticResource headerTemplate}"
ItemTemplate="{StaticResource pivotTemplate}"
Margin="0,-10,0,10" SelectionChanged="galleryPivot_SelectionChanged">
</Pivot>
<Page.Resources>
<DataTemplate x:Key="pivotTemplate">
<Grid>
<TextBlock x:Name="one"></TextBlock>
</Grid>
</DataTemplate>
</Page.Resources>
I want to set the text for the text block namely "one" in the back end.
In the c# end I am doing in the following way.
PivotItem item = (sender as Pivot).ContainerFromItem((sender as Pivot).SelectedItem) as PivotItem;
var rootGrid = item.ContentTemplate.LoadContent() as Grid;
var tb = rootGrid.FindName("one") as TextBlock;
tb.Text = "test";
I am able to get the text block controls and properties of the text block, but when I am trying to set the text in the back end I am not getting anything in the front end where as if I set the property in xaml it is working absolutely fine.
What am I doing wrong?

Disabled TextBox Style

I have created a TextBox above a Path-Element (the Path Element draws a rectangular thing, which acts as the design of the textbox). Now I want to disable this TextBox with
valCon.ValueTextBox.IsEnabled = false;
This works so far. However, since I don't want the TextBox to have any Style (no Color, no Borders, etc.), but only a visible Text in it, I'm getting a small problem:
When the TextBox is disabled, it automatically receives a style which I can't get rid of. The Background changes to white, the Opacity changes to around 0.3 and Borders appear.
I can't seem to solve this problem by adding
valcon.ValueTextBox.Background = new SolidColorBrush(Colors.Transparent);
valcon.ValueTextBox.BorderBrush = new SolidColorBrush(Colors.Transparent);
etc.
It just seems to ignore this. Does anyone know a solution?
Greetings
Narakuvera
You need to take control over the template to achieve the same
here is a basic template for you with no border and no background TextBox
<TextBox Text="hello">
<TextBox.Template>
<ControlTemplate TargetType="TextBox">
<ScrollViewer Margin="0"
x:Name="PART_ContentHost" />
</ControlTemplate>
</TextBox.Template>
</TextBox>
you can choose to set IsEnabled="False" and it will still remain border less
Code behind approach
ControlTemplate ct = new ControlTemplate(typeof(TextBox));
FrameworkElementFactory sv = new FrameworkElementFactory(typeof(ScrollViewer));
sv.Name = "PART_ContentHost";
ct.VisualTree = sv;
textbox1.Template = ct;
WinRT code behind approach
string template = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" TargetType=\"TextBox\"><ScrollViewer Name=\"PART_ContentHost\" /></ControlTemplate>";
ControlTemplate сt = (ControlTemplate)XamlReader.Load(template);
textbox1.Template = сt;

WPF max width of TextBlock in C#

I have need to programmatically create a TextBlock inside a WrapPanel. This TextBlock will act like a heading, therefore I don't want anything to appear to right of the TextBlock. Is there a better way to max out the Width of the TextBlock without doing something like;
myTexblock.Width = 1000000;
Thanks
I think a better solution than putting your header in your WrapPanel is to place the header and WrapPanel in a StackPanel
Something similar to this:
<StackPanel>
<TextBlock Text="Some Header Text"
HorizontalAlignment="Stretch" />
<WrapPanel>
<Button Content="Placeholder" />
<Button Content="Also holding a place" />
</WrapPanel>
</StackPanel>
This gives the same visual effect as what you described without the sloppy property setting.

Categories