How to wrap multiple textbox wrap together? - c#

I want to 1. from the following image:
My code is here:
<WrapPanel>
<TextBlock Text="Title: " Style="{StaticResource Title}" TextWrapping="Wrap" />
<TextBlock Text="{Binding Description" Style="{StaticResource Normal}" TextWrapping="Wrap" />
</WrapPanel>
But if Description text is short, shown like 2., if Description text is long, shown like 3.
How to do this like 1.?

I have solve my question using Run:
<TextBlock TextWrapping="Wrap">
<Run Text="Title: " Style="{StaticResource TitleRun}"/>
<Run Text="{Binding Description,Mode=OneWay}" Style="{StaticResource NormalRun}"/>
</TextBlock>

Just keep adding them to the Grid with 2 columns and n number of rows, adding new rows/colls as you add them.
You can create a behaviour for that.

Related

C# WPF XAML , How create Textbox input field with stringformat?

Code XAML
<Grid>
<StackPanel Orientation="Horizontal">
<Label Content="Bank Doc Nbr :" Margin="0,0,0,0" VerticalAlignment="Center" />
<TextBox Name="txtBankNbr" Text="{Binding ElementName=txtBankNbr, Path=Text, StringFormat={}{0:##0/000}}" materialDesign:HintAssist.Hint="0/000" VerticalAlignment="Center" materialDesign:TextFieldAssist.DecorationVisibility="Hidden" BorderThickness="0" />
<Label Content="Bank Income :€" Margin="5,0,0,0" VerticalAlignment="Center" />
<TextBox Name="txtBankIncome" Text="{Binding ElementName=txtBankIncome, Path=Text,StringFormat={}{0:######0.00}}" materialDesign:HintAssist.Hint="0,00" VerticalAlignment="Center" materialDesign:TextFieldAssist.DecorationVisibility="Hidden" BorderThickness="0" />
</StackPanel>
</Grid>
------ Explanation: Format of txtBankNbr is {0:##0/000} and can only receive numbers. eg "1/001" , 12/021 , ....
Format of txtBankNbr is {0:##0.00} and can only receive a decimal
number What is wrong in my code?
Thanks
Try this:
StringFormat={}{0:#.##}
it should show two decimal place

How to horizontally center an inline (Run) comparing to other inlines in WPF

I have TextBlock in which I put 2 Inlines (Runs), the HorizontalAlignment of the TextBlock is set to Center, this is ok, I just want to center the first Run comparing to the second, here's my code :
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="/Throne;component/Fonts/#Segoe UI Light" FontSize="35" FontWeight="Bold" Visibility="{Binding UserNameTextBlockVisibility}">
<Run FontSize="25">En tant que :</Run>
<LineBreak />
<Run Text="{Binding UserName}" Foreground="ForestGreen"/>
</TextBlock>
Here's the result I get :
What I want to achieve :
I tried to search around the documentation and in threads on the internet, but I didn't really find a way to achieve this, how can I realize that ?
Set TextAlignment="Center" on your TextBlock
<TextBlock TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="/Throne;component/Fonts/#Segoe UI Light" FontSize="35" FontWeight="Bold" Visibility="{Binding UserNameTextBlockVisibility}">
<Run FontSize="25">En tant que :</Run>
<LineBreak />
<Run Text="{Binding UserName}" Foreground="ForestGreen"/>
</TextBlock>

XAML align text in different stackpanels

I'm not sure if what i want is even possible, but here we go. I have multiple stackpanels with multiple lines in them As shown in the following code:
<StackPanel Orientation="Vertical" Margin="700,75,0,0">
<Border>
<TextBlock OpticalMarginAlignment="None">
<Run FontSize="24" Text="Product"></Run><LineBreak/>
<Run FontSize="18" Text="Artikelnummer: "></Run><Run FontSize="18" Text="{Binding ItemNo}"></Run><LineBreak/>
<Run FontSize="18" Text="Omschrijving: "></Run><Run FontSize="18" Text="{Binding Description}"></Run>
</TextBlock>
</Border>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="700,175,0,0">
<Border>
<TextBlock OpticalMarginAlignment="None">
<Run FontSize="24" Text="Prijzen"></Run><LineBreak/>
<Run FontSize="18" Text="Inkoop prijs: "></Run><Run FontSize="18" Text="{Binding PurchasePrice}"></Run><LineBreak/>
<Run FontSize="18" Text="Prijs excl BTW: "></Run><Run FontSize="18" Text="{Binding UnitPriceExclVAT}"></Run><LineBreak/>
<Run FontSize="18" Text="Marge: "></Run><Run FontSize="18" Text="{Binding Margin}"></Run>
</TextBlock>
</Border>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="700,300,0,0">
<Border>
<TextBlock OpticalMarginAlignment="None">
<Run FontSize="24" Text="Extra informatie"></Run><LineBreak/>
<Run FontSize="18" Text="Eenheid: "></Run><Run FontSize="18" Text="{Binding UnitOfMeasure}"></Run><LineBreak/>
<Run FontSize="18" Text="Actuele voorraad: "></Run><Run FontSize="18" Text="{Binding ActualStock}"></Run><LineBreak/>
<Run FontSize="18" Text="Levertijd: "></Run><Run FontSize="18" Text="levertijd"></Run>
</TextBlock>
</Border>
</StackPanel>
As you can see i put 2 texts behind each other. Now i want to allign the second text which start with {Bindin"} all below each other (i hope you understand what i want) Now its shown like this:
header panel 1.
Text1: binding text
longerText1: binding text
evenLongerText1: binding text
header panel 2.
Text2: binding text
longerText2: binding text
evenLongerText2: binding text
But i want this:
header panel 1.
Text1: 'binding text'
longerText1: 'binding text'
evenLongerText1: 'binding text'
header panel 2.
Text2: 'binding text'
longerText2: 'binding text'
evenLongerText2: 'binding text'
Any idea how i can accomplish my goal without just adding spaces (since that will not align nicely)?
Thanks in advance!
Edit: oh i see it doesn't care about my enters and white spaces. let me fix that.
Edit 2: i can't fix it. and can't upload a picture (no 10 rep)
Basically i want every {binding text} to allign nicely.

How can I format text in a TextBlock?

I am trying to make a single block of text composed of multiple TextBlocks
- THE TARGET
For example, I want to achieve like the line below:
"I read this line in a ListBox, notice the multiple text formatting"
The way I'm trying to do is
<StcakPanel Orientation="Horizontal" Width="400" >
<TextBlock Text="I read this line in a " TextWrapping="Wrap" />
<TextBlock Text="ListBox" FontStyle="Italic" TextWrapping="Wrap"/>
<TextBlock Text=", notice the multiple " TextWrapping="Wrap" />
<TextBlock Text="text formatting" FontWeight="Bold" TextWrapping="Wrap"/>
<StcakPanel>
- THE PROBLEM
The text does not fit in the StackPanel, despite of setting TextWrapping for TextBlocks and Width for StackPanel.
I want to generate this code at runtime. I don't know how many words do I need to format.
Kind of showing SearchResults with highlighted search keywords.
- THE QUESTION
How can the above target be achieved either using StackPanel or something else? Having the following constraints.
The text length is unknown
The number of textblocks in the stackpanel is unknown
width and height is Unknown
Thanks very much
You should use a single <TextBlock> which can contain multiple <Run>s that can each have their own formatting. If you want to insert a linebreak, you can use the <Linebreak /> control.
<StackPanel Orientation="Horizontal" Width="400" >
<TextBlock>
<Run Text="I read this line in a" />
<Run Text="ListBox" FontStyle="Italic" />
<Run Text=", notice the multiple" />
<Run Text="text formatting" FontWeight="Bold" />
</TextBlock>
<StackPanel>
At that point you probably don't even need the <StackPanel> unless you are going to have multiple <TextBlocks> stacked on top of one another.
See this post for more information and examples: http://www.danderson.me/posts/working-with-the-wpf-textblock-control/
To databind multiple runs within a TextBlock, see this answer: Databinding TextBlock Runs in Silverlight / WP7

How do I change the font on the bound part of a TextBlock's content?

Given the below TextBlock, how do I make the SomeString part of the text bold?
<TextBlock Text="{Binding SomeString,StringFormat='{}Row: {0}'}" />
ie: If SomeString = "ABC" I want the TextBlock to look like this:
Row: ABC
Try something like this
<StackPanel Orientation="Horizontal">
<TextBlock Text="Row:"/>
<TextBlock FontWeight="Bold" Text="{Binding SomeString}"/>
</StackPanel>
Basically, you could format each individual Run inside the same TextBlock.
Through XAML
<TextBlock>
<Run>Row:</Run>
<Run FontWeight="Bold" Text="{Binding SomeString}"></Run>
</TextBlock>
MSDN Section
Hope this helps.

Categories