WPF中RadGridView的使用和数据绑定

Xaml示例:



    
        
            
                
                
                    
                        
                            
                        
                    
                
                
                
                    
                        
                            
                        
                    
                
                
                
                    
                        
                            
                                
                                
                            
                        
                    
                
            
        
        全部保存
    

Xaml.cs部分

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Telerik.Windows.Controls;

namespace SLShortCut
{
    public partial class MainPage : UserControl
    {
        PersonCollection pc = new PersonCollection();

        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            pc.LstData.Add(new Person() { Name = "張三", Age = 1 });
            pc.LstData.Add(new Person() { Name = "李四", Age = 2 });

            this.DataContext = pc;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            pc.LstData.Add(new Person() { });
        }

        private void btnSub_Click(object sender, RoutedEventArgs e)
        {
            var p = (sender as RadButton).DataContext as Person;
            pc.LstData.Remove(p);

        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in this.pc.LstData)
            {
                MessageBox.Show(string.Format("Name:{0},Age:{1}", item.Name, item.Age));
            }
        }
    }

    public class PersonCollection 
    {
        private ObservableCollection lstData = new ObservableCollection();

        public ObservableCollection LstData
        {
            get { return lstData; }
            set { lstData = value; }
        }
    }

    public class Person : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        private string _name = "";

        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                }
            }
        }

        private int _age = 0;

        public int Age
        {
            get { return _age; }
            set
            {
                _age = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Age"));
                }
            }
        }


    }
}

 

设置行只能单选一行或者多行的方法:修改SelectionMode属性。

你可能感兴趣的:(WPF中RadGridView的使用和数据绑定)