Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate strings instead of using a stack of TextBlocks

I want to show a list of Customer objects in a WPF ItemsControl. I've created a DataTemplate for this:

    <DataTemplate DataType="{x:Type myNameSpace:Customer}">         <StackPanel Orientation="Horizontal" Margin="10">             <CheckBox"></CheckBox>             <TextBlock Text="{Binding Path=Number}"></TextBlock>             <TextBlock Text=" - "></TextBlock>             <TextBlock Text="{Binding Path=Name}"></TextBlock>         </StackPanel>     </DataTemplate> 

So what I want basically is a simple list (with checkboxes) that contains NUMBER - NAME. Isn't there a way in which I can concat the number and name directly in the Binding part?

like image 667
Gerrie Schenck Avatar asked Feb 12 '09 15:02

Gerrie Schenck


People also ask

What is the most efficient way to concatenate many strings together?

When concatenating three dynamic string values or less, use traditional string concatenation. When concatenating more than three dynamic string values, use StringBuilder . When building a big string from several string literals, use either the @ string literal or the inline + operator.

What is the correct way to concatenate the strings?

You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

Can you concatenate more than 2 strings?

Example: Concatenation of strings in columns Note how a nested function is used as the second argument. This is one way to concatenate multiple strings when you have more than two values that you want to use as arguments.

How do I concatenate strings in XAML?

That's why, in this article, we are going to learn how to concatenate strings in our Xaml's, avoiding to declare more graphic controls than needed in our XAML file. It's simple, you just have to add a String. Format, next to the index number inside parenthesis { }, as I show below. More information about String.


1 Answers

There is StringFormat property (in .NET 3.5 SP1), which you probably can use. And usefull WPF binding cheat sheat can found here. If it doesn't help, you can allways write your own ValueConverter or custom property for your object.

Just checked, you can use StringFormat with multibinding. In your case code will be something like this:

<TextBlock>   <TextBlock.Text>     <MultiBinding StringFormat=" {0} - {1}">         <Binding Path="Number"/>         <Binding Path="Name"/>     </MultiBinding>   </TextBlock.Text> </TextBlock> 

I had to start format string with space, otherwise Visual Studio wouldn't build, but I think you will find way get around it :)

Edit
The space is needed in the StringFormat to keep the parser from treating {0} as an actual binding. Other alternatives:

<!-- use a space before the first format --> <MultiBinding StringFormat=" {0} - {1}">  <!-- escape the formats --> <MultiBinding StringFormat="\{0\} - \{1\}">  <!-- use {} before the first format --> <MultiBinding StringFormat="{}{0} - {1}"> 
like image 101
PiRX Avatar answered Sep 19 '22 05:09

PiRX