WPF ComboBox shows typename instead of my display property

Posts   
 
    
happyfirst
User
Posts: 215
Joined: 28-Nov-2008
# Posted on: 13-Sep-2011 05:19:39   

LLBL 3.1, SelfServicing, .Net 4.0

I have an observablecollection<IEntity> of objects on my viewmodel. I've bound the ItemsSource to my viewmodel property in xaml. I've also set DisplayMemberPath and SelectedValuePath. All I get is a combobox that drops down the same typename of my bound objects over and over again.

If I create a datatemplate and use the same property as DisplayMemberPath in a TextBlock, now the correct value is displayed. But, I need to make it work setting DisplayMemberPath since I can do that from code easily.

Seems this happens to a lot of people, and there are always two options. Use DataTemplates or set DisplayMemberPath. Why won't DisplayMemberPath work for me?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 13-Sep-2011 09:45:52   

Please provide the XAML of the ComboBox.

happyfirst
User
Posts: 215
Joined: 28-Nov-2008
# Posted on: 13-Sep-2011 14:11:41   

Sorry.

<ComboBox Width="200" ItemsSource="{Binding Path=StatusTypes}" DisplayMemberPath="ExternalValue" SelectedValuePath="InternalValue" />

All this does is display a drop down full of the typename of what's in the collection of StatusTypes. StatusTypes is a property on the viewmodel of type ObservableCollection<ValidValuesEntity>

If I remove DisplayMemberPath and insert a DataTemplate

<ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding ExternalValue}" /> </DataTemplate> </ComboBox.ItemTemplate>

then it works. But I need to get DisplayMemberPath to work so that I can more easily set it programmatically.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 14-Sep-2011 02:08:24   

Hi there,

I can't reproduce your problem with a very simple Nowthwind example:

ViewModel class

namespace WpfApp
{
    public class CustomerViewModel : DependencyObject, INotifyPropertyChanged
    {
        public CustomerViewModel()
        {
            LoadCustomers();    
        }

        private void LoadCustomers()
        {
            Customers = new ObservableCollection<CustomerEntity>();
            using (var adapter = new DataAccessAdapter())
            {
                var metaData = new LinqMetaData(adapter);
                var temp = metaData.Customer.ToList();

                // fill my ObservableCollection
                foreach (var cust in temp)
                {
                    Customers.Add(cust);
                }
            }           
        }

        public ObservableCollection<CustomerEntity> Customers { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}

Code behind

public partial class MainWindow : Window
{
    CustomerViewModel _viewModel;
    public MainWindow()
    {
        InitializeComponent();

        _viewModel = new CustomerViewModel();
        DataContext = _viewModel;
    }
}

xaml

<ComboBox Height="50" Width="200" ItemsSource="{Binding Path=Customers}" DisplayMemberPath="CompanyName" SelectedValuePath="CustomerId" />

Please show us how are you obtaining the data and binding it to the comboBox.

David Elizondo | LLBLGen Support Team
happyfirst
User
Posts: 215
Joined: 28-Nov-2008
# Posted on: 14-Sep-2011 17:46:42   

I just don't get it. I'm building a predicate, calling getmulti, and then moving the entities into the obvserable collection.

I've just created a bunch of datatemplates and I'm switching the datatemplates programmatically.

WPF problems like these are so frustrating. There's no exceptions being thrown and nothing showing up in the output. Just a drop down with repeating typenames.

Don't worry about this anymore if you can't duplicate. Thanks for the help.