想从铯文件网格绑定列表在XAML与任何code $ c中$ C背后 [英] want to bind List from Cs file to Grid in Xaml with out any code in code behind

查看:121
本文介绍了想从铯文件网格绑定列表在XAML与任何code $ c中$ C背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想结合我的DataGrid这是在XAML文件在收集这是不是在code落后,但在CS文件

我尝试了不同的方法,但没有成功,其实我不想写任何code。在隐藏文件code。

类code

 公共类员工:INotifyPropertyChanged的
{
    私人诠释_id;
    私人字符串_FirstName;
    私人字符串_LastName;
    私人双人_Salary;
    公共事件PropertyChangedEventHandler的PropertyChanged;

    公务员()
    {
    }
    公务员(INT PID,串pFirstName,串pLastName,双pSalary)
    {
        n = PID;
        名字= pFirstName;
        姓氏= pLastName;
        工资= pSalary;
    }

    公众诠释标识
    {
        组
        {
            _id =价值;
            NotifyPropertyChanged(ID);
        }
        {返回_id; }
    }
    公共字符串名字
    {
        组
        {
            _FirstName =价值;
            NotifyPropertyChanged(名字);
        }
        {返回_FirstName; }
    }
    公共字符串名字
    {
        组
        {
            _LastName =价值;
            NotifyPropertyChanged(名字);
        }
        {返回_LastName; }
    }
    公共双薪
    {
        组
        {
            _Salary =价值;
            NotifyPropertyChanged(工资);
        }
        {返回_Salary; }
    }
    私人无效NotifyPropertyChanged(字符串pProperty)
    {
        如果(的PropertyChanged!= NULL)
            的PropertyChanged(这一点,新PropertyChangedEventArgs(pProperty));
    }
}


公共类EmployeeCollection
{
     的ObservableCollection<员工> lstEmp =新的ObservableCollection<员工>();
     公众的ObservableCollection<员工>装getEmployees()
     {
          lstEmp.Add(新员工(1,名字1,姓氏1,1.1));
          lstEmp.Add(新员工(2,名字2,姓氏2,2.2));
          lstEmp.Add(新员工(3,名字3,姓氏3,3.3));
          lstEmp.Add(新员工(4,名字4,姓氏4,4.4));
          lstEmp.Add(新员工(5,名字5,姓氏5,5.5));
          lstEmp.Add(新员工(6,名字6,姓氏6,6.6));

          返回lstEmp;
     }
}
 

它的XAML code

 <我:数据网格的AutoGenerateColumns =真正的保证金=20,0,68,10NAME =dataGrid2高度=135​​VerticalAlignment =底部/&GT ;
 

解决方案

如果我能修改类的方式,对我来说很有意义,我会改变装getEmployees()以静态属性。然后,你可以简单的写(本地是一个命名空间preFIX包含命名空间员工):

 <数据网格的ItemsSource ={X:静态地方:Employee.Employees}/>
 

如果你想保持这个类事情是这样的,首先声明一个的ObjectDataProvider 作为资源(例如,在< Window.Resources> ):

 < ObjectDataProvider的对象类型=本地:雇员方法名=装getEmployees
    X:关键=员工/>
 

,然后使用:

 <数据网格的ItemsSource ={绑定源= {的StaticResource员工}}/>
 

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 success, 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}}" />

这篇关于想从铯文件网格绑定列表在XAML与任何code $ c中$ C背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆