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?

3 Answers 3

7

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; }
}
Sign up to request clarification or add additional context in comments.

Comments

3

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

}

Comments

-3

Check this article.

I think you are missing the ItemsSource= directive.

3 Comments

He states in his question that he assigns the collection to ItemsSource in the code-behind.
This is being set via the code instead of the XAML. That should be OK, shoudln't it?
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.