rrn

I'm trying to append two strings into a textbox without using code behind .. but have been unsuccessful. This is what I have

<DataTemplate x:Key="EmployeeName">

<StackPanel Orientation="Horizontal">

<TextBlock Name="EmployeeFirstNameData" Text="{Binding Path=FirstName}" Style="{StaticResource textStyleTextBlock}"/>

<TextBlock xml:space="preserve"> </TextBlock>

<TextBlock Name="EmployeeLastNameData" Text="{Binding Path=LastName}" Style="{StaticResource textStyleTextBlock}"/>

</StackPanel>

</DataTemplate>

<TextBox Name="EmployeeNameData" />

I want my TextBox to be bound to the dataTemplate so that my textbox will display the full name. How can I do this



Re: Windows Presentation Foundation (WPF) Binding Textbox to a template

Yi-Lun Luo - MSFT

The Text property of a TextBox can only be bound to a string value. You cannot bind it to a StackPanel. You will have to use code behind to create a FullName of type string in the data source, and bind it to the TextBox¡¯s Text property.






Re: Windows Presentation Foundation (WPF) Binding Textbox to a template

rrn

Thank you for the answer.

But it surprises me that you cannot do any kind of string concatenation within xaml. I'm sure there will be scenarios like where you would want to bind a textbox.text property to a some deimal value like a 'price' and prefix the bound value with a '$' sign or any other currency sign. This would be a problem if you are trying to use Content template within your page and the definition for your content template goes to a resource dictionary.





Re: Windows Presentation Foundation (WPF) Binding Textbox to a template

Carole Snyder

This is probably a case where the Label control is more suitable than the TextBlock. A Label is a ContentControl, so it can contain objects other than strings and can use a DataTemplate. Then you can bind your data with pure XAML.




Re: Windows Presentation Foundation (WPF) Binding Textbox to a template

WPCoder

You might also consider using an IValueConverter and binding to the object and doing a format conversion within the Converter. It seems really heavy weight to use a stack panel and several TextBlocks to build a string.

This isn't what you wanted exactly, but it might inspire you: http://www.wiredprairie.us/journal/2007/01/how_to_display_a_formatted_num.html






Re: Windows Presentation Foundation (WPF) Binding Textbox to a template

rrn

Thank you Aaron. I ended up using IMultiValueConverter with MultiBinding and it works well. Carole, I'm also going to try and use Label just to see how that will work. Thank you all for your help