Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Binding Problem

Tags:

c#

binding

wpf

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?

like image 601
Blindy Avatar asked Nov 18 '25 19:11

Blindy


1 Answers

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; }
}
like image 110
Matthias Schippling Avatar answered Nov 20 '25 08:11

Matthias Schippling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!