binding Textblock from two textboxex value in Xaml

273 Views Asked by At

I have two textbox txtFName and txtLName . Now I want to display txtFName - txtLName in textblock using binding.

For bind only txtFName I write below code:

<TextBlock x:Name="textblock" Text="{Binding ElementName=txtFName , Path=Text}" Margin="-3,-8,0,0"/>

But I want to display txtFName - txtLName in textblock using binding.

I do not want to write any code in code behind.

Thanks,

2

There are 2 best solutions below

0
Rohit Vats On BEST ANSWER

You can do that using MultiBinding:

<TextBlock>
    <TextBlock.Text>
        <MultiBinding StringFormat="{}{0} - {1}">
            <Binding ElementName="txtFName" Path="Text"/>
            <Binding ElementName="txtLName" Path="Text"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>
0
AudioBubble On

The content of a TextBlock is composed of a collection of inline objects, e.g. Runs. Therefore, you can bind txtFName and txtLName to two different runs, e.g. like this:

<TextBlock x:Name="textblock">
    <Run Text="{Binding ElementName=txtFName, Path=Text}"/>
    <Run Text=" - "/>
    <Run Text="{Binding ElementName=txtLName, Path=Text}"/>
</TextBlock>