2012-11-10 53 views
0

我有一個Windows應用商店應用項目。如何將類字段綁定到ItemTemplate

我創建了一個物品頁面。這都是默認設置,我想將自定義類綁定到GridView,以便元素可以正確顯示。

GridView的ItemTemplate設置爲Standard250x250ItemTemplate。我想使用該模板並告訴它如何顯示我的自定義元素。我怎樣才能做到這一點?我是否必須創建自己的ItemTemplate?我想用現有的。

順便說一句我正在設置itemGridView.ItemsSource我的收藏。這是告訴GridView要顯示什麼的正確方法嗎?

回答

0

您需要創建項目模板

這裏它使用Obout阿賈克斯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); 
    } 
一個例子,我做了
相關問題