代码之家  ›  专栏  ›  技术社区  ›  Azhar

希望在Xaml中将Cs文件中的列表绑定到网格,而不需要在代码后面添加任何代码

  •  2
  • Azhar  · 技术社区  · 14 年前

    我想将Xaml文件中的DataGrid与不在代码后面但在cs文件中的collection绑定

    我尝试了不同的方法但没有成功,实际上我不想在代码隐藏文件中编写任何代码。

    类别代码

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

    它的Xaml代码

    <my:DataGrid  AutoGenerateColumns="true" Margin="20,0,68,10" Name="dataGrid2" Height="135" VerticalAlignment="Bottom" />
    
    2 回复  |  直到 14 年前
        1
  •  0
  •   svick Raja Nadar    14 年前

    如果我能以对我有意义的方式修改类,我会改变 GetEmployees() 到静态属性。你可以直接写( local 是命名空间的命名空间前缀,其中包含 Employee ):

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

    如果要保持类的原样,请首先声明 ObjectDataProvider 作为一种资源(例如 <Window.Resources> ):

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

    然后使用它:

    <DataGrid ItemsSource="{Binding Source={StaticResource employees}}" />
    
        2
  •  0
  •   peterthegreat    14 年前

    您要做的是将codebhind文件中的DataContext设置为数据源,然后告诉DataGrid绑定到它。是这样做的:

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