Xaphann
Xaphann

Reputation: 3677

Set DisplayMemberPath of ListBox that is bound to an ObservableCollection of ObservableCollection

How do I set the DisplayMemberPath of ListBox that is bound to an ObservableCollection of ObservableCollection's? Everything is working correctly. The ListBox correctly shows the ObservableCollection in it. When the user selects one in the ListBox the data of the ObservableCollection is displayed to DataGrid. The only issue is that ListBox displays (collection) for each ObservableCollection (not something descriptive). Figuring I would need to put a converter in I did this;

<ListBox ItemsSource="{Binding Path=MyCollection}" 
DisplayMemberPath="{Binding Converter={StaticResource CollectionConverter}}" />

Then have a standard converter like this;

public class CollectionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //Convert Logic
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //ConvertBack logic
    }
}

Problem is that the Convert and ConvertBack never fire.

What am I doing wrong?

Upvotes: 0

Views: 157

Answers (1)

shadow32
shadow32

Reputation: 474

Refactor the first collection like this:

class NamedObservableCollection : ObservableCollection<ObservableCollection<MyItem>> 
{  
    public string Name { get; private set;}

    public NamedObservableCollection(string name)
    {
        Name = name;
    }
}

<ListBox ItemsSource="{Binding Path=MyCollection}" DisplayMemberPath="Name"}/>

Upvotes: 1

Related Questions