【问题标题】:How to bind class fields to ItemTemplate如何将类字段绑定到 ItemTemplate
【发布时间】:2012-10-30 12:24:31
【问题描述】:

我有一个 Windows 应用商店应用项目。

我创建了一个项目页面。这都是默认的,我想将我的自定义类绑定到它的 GridView 以便可以正确显示元素。

GridView 的 ItemTemplate 设置为 Standard250x250ItemTemplate。我想使用该模板并告诉它如何显示我的自定义元素。我怎样才能做到这一点?我必须创建自己的 ItemTemplate 吗?我想使用现有的。

顺便说一句,我将itemGridView.ItemsSource 设置为我的收藏。这是告诉GridView 显示什么的正确方法吗?

【问题讨论】:

    标签: c# .net xaml data-binding windows-8


    【解决方案1】:

    您需要创建您的项目模板

    这是我使用 Obout Ajax LIB 完成的示例

    Grid SegmentsGrid = new Grid();
    GridRuntimeTemplate AirlinesTemplate = new GridRuntimeTemplate();
    AirlinesTemplate.ID = "AirlinesTemplate1";
    AirlinesTemplate.ControlID = "AirlinesComboBox1";
    AirlinesTemplate.Template = new Obout.Grid.RuntimeTemplate();
    AirlinesTemplate.Template.CreateTemplate += new Obout.Grid.GridRuntimeTemplateEventHandler(CreateAirlinesTemplate);
    
    GridRuntimeTemplate ClassesTemplate = new GridRuntimeTemplate();
    ClassesTemplate.ID = "ClassesTemplate1";
    ClassesTemplate.ControlID = "ClassesComboBox1";
    ClassesTemplate.Template = new Obout.Grid.RuntimeTemplate();
    ClassesTemplate.Template.CreateTemplate +=new Obout.Grid.GridRuntimeTemplateEventHandler(CreateClassesTemplate);  
    
    SegmentsGrid.Templates.Add(ClassesTemplate);
    SegmentsGrid.Templates.Add(AirlinesTemplate);
    
      public void CreateAirlinesTemplate(Object sender, Obout.Grid.GridRuntimeTemplateEventArgs e)
        {
            PlaceHolder ph1 = new PlaceHolder();
            e.Container.Controls.Add(ph1);
    
    
            AirlinesComboBox.ID = "AirlinesComboBox1";
            AirlinesComboBox.Width = Unit.Percentage(100);
            AirlinesComboBox.Height = Unit.Pixel(200);
            AirlinesComboBox.DataTextField = "Airline_Long_Name";
            AirlinesComboBox.DataValueField = "Airline_Long_Name";
            AirlinesComboBox.EmptyText = "Select Airline ...";
            AirlinesComboBox.EnableLoadOnDemand = true;
            AirlinesComboBox.LoadingItems += AirlinesComboBox1_LoadingItems;
    
            ph1.Controls.Add(AirlinesComboBox);
        }
    
     protected void AirlinesComboBox1_LoadingItems(object sender, ComboBoxLoadingItemsEventArgs e)
        {
            // Getting the countries
            DataTable data = GetAirlines(e.Text);
    
            // Looping through the items and adding them to the "Items" collection of the ComboBox
            for (int i = 0; i < data.Rows.Count; i++)
            {
                (sender as ComboBox).Items.Add(new ComboBoxItem(data.Rows[i]["Airline_Long_Name"].ToString(), data.Rows[i]["Airline_Long_Name"].ToString()));
            }
    
            e.ItemsLoadedCount = data.Rows.Count;
            e.ItemsCount = data.Rows.Count;
        }
    
    
      protected void ClassesComboBox1_LoadingItems(object sender, ComboBoxLoadingItemsEventArgs e)
        {
            // Getting the countries
            DataTable data = GetClasses(e.Text);
    
            // Looping through the items and adding them to the "Items" collection of the ComboBox
            for (int i = 0; i < data.Rows.Count; i++)
            {
                (sender as ComboBox).Items.Add(new ComboBoxItem(data.Rows[i]["Class_Name"].ToString(), data.Rows[i]["Class_Name"].ToString()));
            }
    
            e.ItemsLoadedCount = data.Rows.Count;
            e.ItemsCount = data.Rows.Count;
        }
    
        public void CreateClassesTemplate(Object sender, Obout.Grid.GridRuntimeTemplateEventArgs e)
        {
            PlaceHolder ph1 = new PlaceHolder();
            e.Container.Controls.Add(ph1);
    
    
            ClassesComboBox.ID = "ClassesComboBox1";
            ClassesComboBox.Width = Unit.Percentage(100);
            ClassesComboBox.Height = Unit.Pixel(200);
            ClassesComboBox.DataTextField = "Class_Name";
            ClassesComboBox.DataValueField = "Class_Name";
            ClassesComboBox.EmptyText = "Select Flight Class ...";
            ClassesComboBox.EnableLoadOnDemand = true;
            ClassesComboBox.LoadingItems += ClassesComboBox1_LoadingItems;
            ph1.Controls.Add(ClassesComboBox);
        }
    

    【讨论】:

      猜你喜欢
      • 2016-07-02
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 2012-09-02
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      相关资源
      最近更新 更多