3

This is my current code:

<StackPanel Grid.Column="4" Grid.Row="1"> <TextBlock Text="{Binding Strain}" /> <TextBlock Text="/" /> <TextBlock Text="{Binding MaximumStrain}" /> </StackPanel> 

What I would like to do is have one TextBlock with the expression "{Strain}/{MaximumStrain}"

4
  • P.S. I don't literally need a TextBlock. Another control that is visually similar will work as well. Commented Jul 4, 2016 at 23:36
  • Just a quick thought. Could bind it to a list/collection and use converter? Commented Jul 5, 2016 at 1:04
  • Hard to pick a winner. Both answers have their advantages and limitations. Commented Jul 5, 2016 at 2:12
  • Nice one. This question should be quite useful for many now. Thanks Jonathan for pointing out pros and cons of each method :) Commented Jul 5, 2016 at 2:24

2 Answers 2

9

Well you could use the "Run" command, try this:

 <StackPanel> <TextBlock> <Run Text="{Binding Strain}"></Run> <Run Text="/"></Run> <Run Text="{Binding MaximumStrain}"></Run> </TextBlock> </StackPanel> 

Pros: Can change font settings for each Run.

To avoid adding an empty space between your text all you have to do is place the Run commands in the same line like this:

<Run Text="{Binding Strain}"></Run><Run Text="/"></Run><Run Text="{Binding MaximumStrain}"></Run> 
Sign up to request clarification or add additional context in comments.

Comments

9

Use Multibinding with stringformat.

<TextBlock Grid.Column="4" Grid.Row="1"> <TextBlock.Text> <MultiBinding StringFormat="{}{0}/{1}"> <Binding Path="Strain" /> <Binding Path="MaximumStrain" /> </MultiBinding> </TextBlock.Text> </TextBlock> 

Pros: Direct control over spacing.

Cons:

  • No complex font styling.
  • Needs Mode=OneWay in some cases. May throw an error or silently fail to bind depending on the use case.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.