views:

61

answers:

1

I Have A wpf UserControl with a property:

private IEnumerable<PropertyBase> properties;
public IEnumerable<PropertyBase> Properties
{
 get {return properties;}
 set
 {
  properties = from property in value
    orderby property.Position
    select property;
 }
}

I want to create a ListBox that is bound to my Properties property with Property.Name as the displayed value, all the examples I'm finding use DataProviders in separate classes and appear to overcomplicate the situation. Is there a simpler way of achieving this.

I tried the following but no data was displayed:

<ListBox Name="propertiesListBox" ItemsSource="{Binding Source=this, Path=Properties}" DisplayMemberPath="Name" />
+3  A: 
<ListBox ItemsSource="{Binding Properties}" DisplayMemberPath="Name"/>

Assumes your DataContext is the UserControl instance.

HTH, Kent

Kent Boogaart
This may sound stupid but how do I set the DataContext to the UserControl instance. If I have <ListBox DataContext="this" DaItemsSource="{Binding Properties}" DisplayMemberPath="Name"/> I still have the same problem
Mark
Easiest way would be to set DataContext = this in the UserControl constructor. Or you could set DataContext="{RelativeSource Self}" on the <UserControl element in the XAML.
Kent Boogaart