David Brunelle
David Brunelle

Reputation: 6450

WPf : Binding with more than one property at a time

I have a list view in which I use binding to display my information. I use a simple data template. Is there a way to bind two data in one control. What I mean would to be replace something like :

<TextBlock Text="{Binding LName}"/>

<TextBlock Text=", "/>

<TextBlock Text="{Binding NName}"/>

to something like

<TextBlock Text="{Binding LName} + ',' + {Binding FName}"/>

Thanks

Upvotes: 3

Views: 3702

Answers (2)

Kent Boogaart
Kent Boogaart

Reputation: 178770

If you're on WPF 3.5SP1 or above, you don't need to write your own value converter for your use case. Instead, just use StringFormat:

<TextBlock>
  <TextBlock.Text>
    <MultiBinding StringFormat="{}{0}, {1}">
      <Binding Path="LName" />
      <Binding Path="FName"/>
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

Upvotes: 10

Reed Copsey
Reed Copsey

Reputation: 564641

Yes. You would use a MultiBinding along with an IMultiValueConverter.

The MultiBinding help shows an example doing exactly what you're attempting - binding one text box to first + last names.

Upvotes: 5

Related Questions