Polaris
Polaris

Reputation: 3793

Bind to several class properties

I have some class with properties firstName and lastName. I want bind TextBlock to concatanation of this two properties. I know that I can create third property that will be return concatanation of these properties. But I dont want to use this approach. Is it possible to Bind TextBlock to two properties. and also I dont want create composite userControl.

Upvotes: 1

Views: 179

Answers (4)

Daniel Rose
Daniel Rose

Reputation: 17638

In .NET 3.5SP1, Microsoft added StringFormat to bindings. This makes it much easier. See Lester's blog post for an example. In your case:

<TextBox>
  <TextBox.Text>
    <MultiBinding StringFormat="{0} {1}">
      <Binding Path="FirstName" />
      <Binding Path="LastName"/>
    </MultiBinding>
  </TextBox.Text>
</TextBox>

or

<TextBox>
  <TextBox.Text>
    <MultiBinding StringFormat="{1}, {0}">
      <Binding Path="FirstName" />
      <Binding Path="LastName"/>
    </MultiBinding>
  </TextBox.Text>
</TextBox>

Upvotes: 3

Kishore Kumar
Kishore Kumar

Reputation: 21863

use either MultiBinding or Converter (if complex operation is there)

Upvotes: 0

Oppermann
Oppermann

Reputation: 78

You could use multibinding, but I guess that you have to code your way out of the concatanation. Here is an example: Multibinding

Upvotes: 2

Amry
Amry

Reputation: 4971

I'm not sure if it's possible to bind to two properties, but there is not reason you cannot create two TextBlocks right?

<TextBlock Text="{Binding firstName}"/> <TextBlock Text="{Binding lastName}"/>

Upvotes: 0

Related Questions