WPF Binding Problem

binding, c#, wpf

Solution

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; }
}

Problem

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?

Original source