zoukankan      html  css  js  c++  java
  • Telerik 控件 RadGridview实现绑定

    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>

      完成,显示效果为:

      

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

  • 相关阅读:
    Umbraco中更换IndexSet中的NodeType后,搜索页面没有做出对应更改的效果
    Umbraco部署到IIS中权限问题(back office没有权限新建template)
    C控制台密码输入:输入一个字符显示一个星号
    C项目实践--家庭财务管理系统
    C 编程中fseek、ftell的用法总结
    C ++模板的声明和实现为何要放在头文件中?
    头文件与cpp文件为什么要分开写
    printf、sprintf与fprintf 的用法区分
    C编程中fread 、fwrite 用法总结
    C从控制台(stdin)输入带空格的字符串到字符数组中
  • 原文地址:https://www.cnblogs.com/qq247039968/p/4060951.html
Copyright © 2011-2022 走看看