Telerik radgridview控件(一)

最近手头有一个WPF项目,项目要画多个表格和多个复杂图。便想到了用Telerik控件来实现绘制表格和绘制复杂图。刚开始入手走了不少弯路,发个博客总结一下。

参考链接:Telerik官方文档

官方Demo:

1.在Xaml中添加引用 :

xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"

  在Grid中添加radgridview控件:

<Grid Background="#EBEDEF">
    <telerik:RadGridView />
</Grid>

此时界面显示一个空网格:


2. 数据绑定

官网demo中为了填充数据,创建了业务对象Club(后台cs文件中定义):

public class Club : INotifyPropertyChanged 
{
    public event PropertyChangedEventHandler PropertyChanged;
    // 字段
    private string name;
    private DateTime established;
    private int stadiumCapacity;
    // 封装
    public string Name
    {
        get { return this.name; }
        set
        {
            if (value != this.name)
            {
                this.name = value;
                this.OnPropertyChanged("Name");
            }
        }
    }

    public DateTime Established
    {
        get { return this.established; }
        set
        {
            if (value != this.established)
            {
                this.established = value;
                this.OnPropertyChanged("Established");
            }
        }
    }

    public int StadiumCapacity
    {
        get { return this.stadiumCapacity; }
        set
        {
            if (value != this.stadiumCapacity)
            {
                this.stadiumCapacity = value;
                this.OnPropertyChanged("StadiumCapacity");
            }
        }
    }
    // 构造函数
    public Club(string name, DateTime established, int stadiumCapacity)
    {
        this.name = name;
        this.established = established;
        this.stadiumCapacity = stadiumCapacity;
    }
    // 实现 INotifyPropertyChanged 接口
    protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, args);
        }
    }

    private void OnPropertyChanged(string propertyName)
    {
        this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }
}

    TIPS: 如果想使用双向绑定,除了在radgridview控件中设定绑定模式外,还应实现 INotifyPropertyChanged接口

3. 构造ObeservableCollection

在构造函数中构造ObservableCollection:

public class MyViewModel : ViewModelBase
{
    private ObservableCollection<Club> clubs;

    public ObservableCollection<Club> Clubs
    {
        get
        {
            if (this.clubs == null)
            {
                this.clubs = this.CreateClubs();
            }

            return this.clubs;
        }
    }

    private ObservableCollection<Club> CreateClubs()
    {
        ObservableCollection<Club> clubs = new ObservableCollection<Club>();
        Club club;
        // 添加几条数据
        club = new Club("Liverpool", new DateTime(1892, 1, 1), 45362);
        clubs.Add(club);

        club = new Club("Manchester Utd.", new DateTime(1878, 1, 1), 76212);
        clubs.Add(club);

        club = new Club("Chelsea", new DateTime(1905, 1, 1), 42055);
        clubs.Add(club);

        return clubs;
    }
}

4. 在radgridview控件中进行数据的绑定

<Grid.Resources>
    <local:xxxxViewModel x:Key="MyViewModel" />
</Grid.Resources>
<telerik:RadGridView x:Name="gridView" DataContext="{StaticResource MyViewModel}" ItemsSource="{Binding Clubs}"/>
运行成功后,结果如下:


5. 自己绑定列

上面的表格中,每一列都由radgridview控件自动生成,默认是绑定的封装的名称。如果想自己去设定列的名称,需要手动去绑定列。首先设置属性 AutoGenerateColumn="False",接着自己绑定表格的列:

<telerik:RadGridView x:Name="gridview" DataContext="{StaticResource MyViewModel}" ItemSource="{Binding Clubs}" AutoGenerateColumns="False" >
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" Header="Club Name"/>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Established}" Header="Established"/>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

运行结果如下:


Telerik的radgridview控件实际上是对WPF datagrid的扩充,功能上比WPF datagrid更强。更多更具体的操作,详见Telerik官网。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值