It is show the name of the class when I use a template in a combobox

ComptonAlvaro 166 Reputation points
2022-07-11T11:46:33.337+00:00

I want to update the text of an item in a combobox when the item is saved.

Although the entity that I use as source implements the notify property changed event, it is not update. I have read the solution it is to use a data templeate instead of DisplayMemberPath.

So I am using this:

<DataTemplate x:Key="TipoComponenteTemplate"  
          DataType="{x:Type clases:TiposComponentesUI}">  
    <TextBlock Text="{Binding TipoComponente}"/>  
</DataTemplate>  
  
  
        <ComboBox Name="cmbTiposComponentes" Width="150"  
                  Text="{Binding TiposComponentesTexto, UpdateSourceTrigger=PropertyChanged}"  
                  ItemsSource="{Binding TiposComponentes}"  
                  ItemTemplate="{StaticResource ResourceKey=TipoComponenteTemplate}"  
                  SelectedItem="{Binding TiposComponentesSelectedItem}">  

That works partially, in the way when I open the combobox, the items show the correct text, but when I select one of them, in the textbox of the combobox it is shown the name of the class instead of the data in the property TipoComponente that is defined in the data template.

So I would like to know how I could show the same text in the textbox than the text that is shown in the items.

Thanks.

EDIT: it is seems because the combobox is editable.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 51,346 Reputation points
    2022-07-11T14:13:12.21+00:00

    You didn't provide the class definition for the code backing this binding so we're sort of guessing here. Given that the defacto standard is to call ToString to get the data to display in the UI I suspect you're binding the output control (textbox?) to the object instead of the string property on the object. Just a cursory glance but your combobox Text property is binding to TiposComponentsTexto which I suspect is the string property with the value you want. Change your TextBlock' s binding property to bind to the same property and see if the problem goes away. Alternatively implement ToString on the type and have it return this property value instead. Then you don't need to change the binding.

    0 comments No comments