Telerik 控件 RadGridview实现绑定

时间:2022-10-25 16:34:31

0.使用MiroModes框架请先阅读http://blogs.telerik.com/blogs/posts/10-01-20/micromodels_for_silverlight.aspx

  1.首先定义模型

 public class Division
    {
        public int Id
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public List<Team> Teams
        {
            get;
            set;
        }

}

    public class Team
    {
        public int Id
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public int Place
        {
            get;
            set;
        }

}

  2.定义service

 public class DivisionsService
    {
        public static ObservableCollection<Division> GetDivisions()
        {
            ObservableCollection<Division> divisions = new ObservableCollection<Division>();
            Division dA = new Division();
            dA.Name = "Division A";
            dA.Id = 1;
            dA.Teams = new List<Team>();
            Team team1 = new Team();
            team1.Id = 1;
            team1.Name = "Team I";
            team1.Place = 1;
            dA.Teams.Add(team1);
            Team team2 = new Team();
            team2.Id = 2;
            team2.Name = "Team II";
            team2.Place = 2;
            dA.Teams.Add(team2);
            Team team3 = new Team();
            team3.Id = 3;
            team3.Name = "Team III";
            team3.Place = 3;
            dA.Teams.Add(team3);
            divisions.Add(dA);
            Division dB = new Division();
            dB.Name = "Division B";
            dB.Id = 2;
            dB.Teams = new List<Team>();
            Team teamRed = new Team();
            teamRed.Id = 1;
            teamRed.Name = "Team Red";
            teamRed.Place = 1;
            dB.Teams.Add(teamRed);
            Team teamGreen = new Team();
            teamGreen.Id = 2;
            teamGreen.Name = "Team Green";
            teamGreen.Place = 2;
            dB.Teams.Add(teamGreen);
            Team teamBlue = new Team();
            teamBlue.Id = 3;
            teamBlue.Name = "Team Blue";
            teamBlue.Place = 3;
            dB.Teams.Add(teamBlue);
            divisions.Add(dB);

Division dC = new Division();
            dC.Name = "Division C";
            dC.Id = 3;
            dC.Teams = new List<Team>();
            Team teamAlpha = new Team();
            teamAlpha.Id = 1;
            teamAlpha.Name = "Team Alpha";
            teamAlpha.Place = 1;
            dC.Teams.Add(teamAlpha);
            Team teamBeta = new Team();
            teamBeta.Id = 2;
            teamBeta.Name = "Team Beta";
            teamBeta.Place = 2;
            dC.Teams.Add(teamBeta);
            Team teamGama = new Team();
            teamGama.Id = 3;
            teamGama.Name = "Team Gama";
            teamGama.Place = 3;
            dC.Teams.Add(teamGama);
            divisions.Add(dC);
            return divisions;
        }

}

  3.定义viewModel

public class Viewmodel : MicroModel
    {
        public Viewmodel(ObservableCollection<Division> divisions)
        {
            //AllProperties(order);

Collection("LineItems", () => divisions);
            //.Each((item, model) => model.Property("LineTotal", () => item.UnitPrice * item.Quantity));

//Command("Save", () => orderService.Save(order, lineItems));
        }
    }

  4.定义view

 <Grid x:Name="LayoutRoot" DataContext="{Binding Object}">
<telerik:RadGridView  AutoGenerateColumns="False" x:Name="HierarchicalGridView" ItemsSource="{Binding Path=LineItems}">
            <telerik:RadGridView.ChildTableDefinitions>

<telerik:GridViewTableDefinition>
                    <telerik:GridViewTableDefinition.Relation>
                        <telerik:PropertyRelation ParentPropertyName="Teams" />
                    </telerik:GridViewTableDefinition.Relation>
                </telerik:GridViewTableDefinition>

</telerik:RadGridView.ChildTableDefinitions>

<telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="Id">
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Grid DataContext="{Binding Object}" >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="80" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Margin="1" Grid.Column="0" Text="{Binding Path=Id}" />
                            </Grid>
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Name">
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Grid DataContext="{Binding Object}" >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="80" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Margin="1" Grid.Column="0" Text="{Binding Path=Name}" />
                            </Grid>
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>

</telerik:RadGridView>

  完成,显示效果为:

  

Telerik 控件 RadGridview实现绑定

  本文来自xiaoguang44的博客,原文地址:http://blog.csdn.net/xiaoguang44/article/details/6818480