How do I bind data in a ListView?

Developer
Aug 12, 2009 at 12:01 AM

The code below works for the ListView. When I set the ItemsSource on it to an array of objects with 3 properties same name as the Header.

Each column gets a hash table with the key value pairs for all properties rather than data binding taking effect the single property.

Do I need to set up the data binding for the list view to get that to work?

        GroupBox -Header Result -BorderThickness 0 {
            ListView -Name lstResults -MinHeight 100 -View {
                GridView -AllowsColumnReorder -Columns {
                    GridViewColumn -Header "Subject"
                    GridViewColumn -Header "Predicate"
                    GridViewColumn -Header "Object"
                }
            }
        }

Coordinator
Aug 12, 2009 at 1:46 AM
Edited Aug 12, 2009 at 1:50 AM

The problem is that you didn’t actually bind the columns to a property, so you just get the .ToString() of the object. 

Try setting the -DisplayMemberBinding to a script block like: { Binding -Path “Subject” }


$data = @{ 
    Subject = "This is a Subject" 
    Predicate = "This is a Predicate" 
    Object = "This is an Object" 
}, @{ 
    Subject = "Cup"
    Predicate = "Runs"
    Object = "Over"
}   | ConvertFrom-Hashtable

 
Boots -Width 1000 -Height 725 -WindowStartupLocation CenterScreen {
    GroupBox -Header Result -BorderThickness 0 {
        ListView -Name lstResults -MinHeight 100 -View {
            GridView -AllowsColumnReorder -Columns {
                GridViewColumn -Header "Subject" -DisplayMemberBinding { Binding -Path "Subject" }
                GridViewColumn -Header "Predicate" -DisplayMemberBinding { Binding -Path "Predicate" }
                GridViewColumn -Header "Object" -DisplayMemberBinding { Binding -Path "Object" }
            }
        } -ItemsSource $data
    }
 }