Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF how to bind gridview?

Tags:

binding

wpf

i am doing this xaml :

 <StackPanel Margin="320,0,0,0" Grid.RowSpan="2">
        <ListView ItemsSource="{Binding employeeCollection}">
            <ListView.View>
                <GridView>

                    <GridViewColumn Header="Employee ID" DisplayMemberBinding="{Binding Path=EmployeeID}"/>
                    <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Path=FirstName}"/>
                    <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Path=LastName}"/>
                    <GridViewColumn Header="start" DisplayMemberBinding="{Binding Path=startHR}"/>
                    <GridViewColumn Header="finish" DisplayMemberBinding="{Binding Path=finishHR}">

                </GridViewColumn>
            </GridView>
    </ListView.View>

        </ListView>
    </StackPanel>

and the code behind is :

class employeesGrid //: INotifyPropertyChanged
{
    ObservableCollection<employiesData> _employeeCollection = 
    new ObservableCollection<employiesData>();

    public employeesGrid()
{
    _employeeCollection.Add(new employiesData{

      EmployeeID = "World Of Warcraft", 
      FirstName = "Blizzard", 
      LastName = "Blizzard",
      startHR = "2222",
      finishHR = "dfs"
  });


}

    public ObservableCollection<employiesData> employeeCollection
{ get { return _employeeCollection; } }


}

public class employiesData
{
    public string EmployeeID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string startHR { get; set; }
    public string finishHR { get; set; }
}

}

where inside my main window i am doing :

//constructor: InitializeComponent(); employeesGrid em = new employeesGrid();

1.can someone please guide me what am i doing wrong ? 2.INotifyPropertyChanged why should i use it ? how should i use it ?

thanku for gazing in my work it means a lot to me :)

lets say i want two sturctures like this in my program what would be the best implmantion ????

like image 516
yoav.str Avatar asked Apr 02 '26 02:04

yoav.str


2 Answers

You never set your listviews' DataContext.

Try this in your window constructor:

InitializeComponent(); 
employeesGrid em = new employeesGrid();
this.DataContext = em;
like image 151
treehouse Avatar answered Apr 03 '26 16:04

treehouse


  1. You need to bind your view's datasource to your class instance. In your constructor, do this: this.DataContext = new employeesGrid();
  2. INotifyPropertyChanged is an interface that you should use if you want your UI to refresh it's content when the underlying content changes.
like image 20
OJ. Avatar answered Apr 03 '26 17:04

OJ.



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!