开发者

want to bind List from Cs file to Grid in Xaml with out any code in code behind

I want to bind my DataGrid which is in Xaml file with collection which is not in the Code behind but in a cs file

I tried different methods but no succ开发者_运维问答ess, Actually I do not want to write any code in Code behind file.

class Code

public class Employee : INotifyPropertyChanged
{            
    private int _Id;
    private string _FirstName;
    private string _LastName;
    private double _Salary;
    public event PropertyChangedEventHandler PropertyChanged;

    public Employee()
    {
    }
    public Employee(int pId, string pFirstName, string pLastName, double pSalary)
    {
        Id = pId;
        FirstName = pFirstName;
        LastName = pLastName;
        Salary = pSalary;
    }

    public int Id
    {
        set 
        { 
            _Id = value;
            NotifyPropertyChanged("Id");
        }
        get { return _Id; }
    }
    public string FirstName
    {
        set 
        { 
            _FirstName = value;
            NotifyPropertyChanged("FirstName");
        }
        get { return _FirstName; }
    }
    public string LastName
    {
        set 
        { 
            _LastName = value;
            NotifyPropertyChanged("LastName");
        }
        get { return _LastName; }
    }
    public double Salary
    {
        set 
        {             
            _Salary = value;
            NotifyPropertyChanged("Salary");
        }
        get { return _Salary; }
    }
    private void NotifyPropertyChanged(string pProperty)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(pProperty)); 
    }
}


public class EmployeeCollection
{
     ObservableCollection<Employee> lstEmp = new ObservableCollection<Employee>();
     public ObservableCollection<Employee> GetEmployees()
     {
          lstEmp.Add(new Employee(1,"Firstname 1", "Lastname 1", 1.1 ));
          lstEmp.Add(new Employee(2, "Firstname 2", "Lastname 2", 2.2));
          lstEmp.Add(new Employee(3, "Firstname 3", "Lastname 3", 3.3));
          lstEmp.Add(new Employee(4, "Firstname 4", "Lastname 4", 4.4));
          lstEmp.Add(new Employee(5, "Firstname 5", "Lastname 5", 5.5));
          lstEmp.Add(new Employee(6, "Firstname 6", "Lastname 6", 6.6));

          return lstEmp;
     }
}

its Xaml code

<my:DataGrid  AutoGenerateColumns="true" Margin="20,0,68,10" Name="dataGrid2" Height="135" VerticalAlignment="Bottom" />


If I could modify the class in a way that makes sense to me, I would change GetEmployees() to a static property. You could then simply write (local is a namespace prefix for the namespace containing Employee):

<DataGrid ItemsSource="{x:Static local:Employee.Employees}" />

If you want to keep the class the way it is, first declare a ObjectDataProvider as a resource (e.g. in <Window.Resources>):

<ObjectDataProvider ObjectType="local:Employee" MethodName="GetEmployees"
    x:Key="employees" />

And then use that:

<DataGrid ItemsSource="{Binding Source={StaticResource employees}}" />


What you do is you set the DataContext in the codebehind file to your datasource and then tell your DataGrid to bind to that. It's done like this:

C#:

public MainWindow()
        {
            InitializeComponent();

            Employee emp = new Employee();
            DataContext = emp.GetEmployees();

        }

XAML:

<DataGrid  AutoGenerateColumns="true" Margin="20,0,68,10" Name="dataGrid2" Height="135" VerticalAlignment="Bottom" ItemsSource="{Binding}" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜