tags:

views:

143

answers:

3

I have this object:

    class a 
    { 
        public string Application; 
        public DateTime From, To;
    }

And I declare this list with it:

    ObservableCollection<a> ApplicationsCollection = 
        new ObservableCollection<a>();

In my XAML I have:

    <ListView Height="226.381" Name="lstStatus" Width="248.383" HorizontalAlignment="Left" Margin="12,0,0,12" VerticalAlignment=">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="140" Header="Application"
                                DisplayMemberBinding="{Binding Path=Application}"/>
                <GridViewColumn Width="50" Header="From" 
                                DisplayMemberBinding="{Binding Path=From}"/>
                <GridViewColumn Width="50" Header="To" 
                                DisplayMemberBinding="{Binding Path=To}"/>
            </GridView>
        </ListView.View>
    </ListView>

When I do:

        lstStatus.ItemsSource = ApplicationsCollection;

I get a bunch of errors and nothing shows up in my list view:

System.Windows.Data Error: 39 : BindingExpression path error: 'Application' property not found on 'object' ''a' (HashCode=60325168)'. BindingExpression:Path=Application; DataItem='a' (HashCode=60325168); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 39 : BindingExpression path error: 'From' property not found on 'object' ''a' (HashCode=60325168)'. BindingExpression:Path=From; DataItem='a' (HashCode=60325168); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 39 : BindingExpression path error: 'To' property not found on 'object' ''a' (HashCode=60325168)'. BindingExpression:Path=To; DataItem='a' (HashCode=60325168); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

It's obviously seeing the object as having type a and a's obviously have the correct properties, so why isn't this working?

A: 

Check this article - http://www.codeproject.com/KB/miscctrl/GridView%5FWPF.aspx I think you are missing the ItemsSource= directive.

Dani
He states in his question that he assigns the collection to ItemsSource in the code-behind.
Matt Hamilton
This is being set via the code instead of the XAML. That should be OK, shoudln't it?
Andrew Shepherd
It should be ok, but try to move it to xaml.I've missed that line in the question, as Matt mentioned.maybe you need to mark the class as public as well...
Dani
+3  A: 

Ok you use fields but you need properties

class a 
{ 
    public string Application
    {
       get;set;
    }
    public DateTime From
    {
       get;set;
    } 
    public DateTime To
    {
       get;set;
    } 

}
Trickster
+4  A: 

Looks like WPF can't bind to fields directly, you have to use properties like so:

class a
{
    public string Application { get; set; }
    public DateTime From { get; set; }
    public DateTime To { get; set; }
}
Matthias Schippling
Wow this was driving me nuts, thanks a bunch!
Blindy