views:

988

answers:

3
A: 

Try using ItemsSource="{Binding}" instead.

mdm20
That got me closer.. I got an error that said "Items collection must be empty before using ItemsSource." so i wiped out my gridview item and then i got a whole slew of System.Data.DataRowView rows in my Listview
Rob
I think the problem is that the ListView is confusing your GridView for an item. You need to use add the following tag around your GridView. <ListView.View></ListView.View>
mdm20
+1  A: 

Hi Rob,

I think part of the problem is you are still thinking WinForms. Instead of binding a Grid to a Table, how about binding the ListBox to a Collection?

Try this instead:

1) Create another class that implements INotifyPropertyChanged. We are going to use this class for the DataContext. Think of it as your BindingEngine. I typically make this the DataContext of the Window itself, making it available anywhere in the Window.

2) Expose a property in the new class that is an ObservableCollection, where YourType is another class that implements INotifyPropertyChanged and exposes the data properties you wish to display.

3) Create a method in the Engine that populates your Collection. Then fire the PropertyChanged event when it is populated.

4) Bind the ListBox ItemsSource to the Property.

5) Create an ItemTemplate and use the property names from YourType for the Binding.

This is psuedo-code, but should get you close:

<Window>
    <Window.Resources>
        <ObjectDataProvider x:Key="MyEngineDS" ObjectType="{x:Type MyEngine:MyEngineNamespace}" d:IsDataSource="True"/>
        <DataTemplate x:Key="ItemTemplate1">
            <TextBlock Text="{Binding MyPropertyName}" />
        </DataTemplate>
    </Window.Resources>
    <Window.DataContext>
        <Binding Mode="OneWay" Source="{StaticResource MyEngineDS}"/>
    </Window.DataContext>
    <Grid x:Name="LayoutRoot">
        <ListBox ItemsSource="MyCollection" ItemTemplate="{DynamicResource ItemTemplate1}" />
    </Grid>
</Window>

Hope this helps.

Joel Cochran
+5  A: 

I made a sample app and discovered you need to surround your GridView with ListView.View and set your ItemsSource to {Binding} just like the following:

<ListView Name="Preview" ItemsSource="{Binding}">
<ListView.View>
  <GridView>
    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=FormalName}" />
  </GridView>
 </ListView.View>
</ListView>
statenjason
Good answer, and welcome to the community.
JoshJordan
I love this place... Thanks guys. I really need to buy a WPF book a learn all this new stuff properly.
Rob
Check out WPF Unleashed. It's very informative, and helped me in my WinForms to WPF transition.
statenjason