WPF Binding Problem

I have this object:

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

And I declare this list:

    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 this:

        lstStatus.ItemsSource = ApplicationsCollection;

I get a bunch of errors and nothing appears in my list:

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')

Obviously, the object is of type aand obviously has the correct properties, so why doesn't this work?

+3
source share
3 answers

It seems that WPF cannot directly communicate with fields, you should use the following properties:

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

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

}
+3
source

Source: https://habr.com/ru/post/1721007/


All Articles