0

I have the following ComboBox in my project, but one thing is bothering me very much, if I write it like this:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="170" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Label Content="Candidate" Margin="3" Foreground="White" Grid.Column="0"/>
    <ComboBox ItemsSource="{Binding CandidateList, UpdateSourceTrigger=PropertyChanged}" 
              SelectedItem="{Binding Candidate, UpdateSourceTrigger=PropertyChanged}"
              IsEditable="True" Margin="3" Grid.Column="1">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock>
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}{0} {1}">
                            <Binding Path="FirstName"/>
                            <Binding Path="SecondName"/>
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</Grid>

So the names in the unfolded version get formatted right, but in the ComboBox itself it displays it not right ...

Is there any possibility to make this work in xaml ?

Snaylor
  • 43
  • 1
  • 7

2 Answers2

2

This question has been asked before.

WPF IsEditable=true ComboBox filled with objects displays the ToString() as the selected item

WPF Combobox: Different template in textbox and drop-downlist

You could add a property to your data object that returns the full name and set the TextSearch.TextPath property of the ComboBox to this property or try to modify the ControlTemplate of the ComboBoxItem container. Please check the above links for more information.

Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88
0

Just a thought, but you could solve this, and remove the need for future converters when you want to display this, by adding a DisplayName property with a getter (will need to add OnPropertyChanged("DisplayName") to your First/Second name setters), or by overriding the ToString() method on your candidate class. basically -

Override ToString()
{
    return FirstName + " " + SecondName ;
}

Which actually I'm not sure if it'll be triggered by OnPropertyChanged if you change the values :/

Oyiwai
  • 449
  • 1
  • 3
  • 20