roybalderama
roybalderama

Reputation: 1640

Concatenation of items in list to use in WPF combobox Itemsource

I have a List where a Person has Firstname and Surname. I want to concatenate Firstname and Surname to set combobox's itemsource and Display member Path.

Upvotes: 2

Views: 1100

Answers (2)

Shaminder Singh
Shaminder Singh

Reputation: 1293

You can use DataTemplate for Concatinating two properties. For Example.

<ComboBox>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <Run Text="{Binding TaxName}" />
                <Run Text=" | @" />
                <Run FontWeight="Bold" Text="{Binding Rate}" />
                <Run  FontWeight="Bold" Text="%" />
            </TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

it will be viewed as: Service Tax | @12.36% and bind its value on the code behind as

cmbTax.ItemsSource = taxes;
cmbTax.SelectedValuePath = "TaxID";

Upvotes: 3

estebane97
estebane97

Reputation: 1138

Just create a view model, add a property with FirstName + Surname and use it in Display Member path.

Upvotes: 4

Related Questions