2014-02-20 29 views
0

嗨我有一個Pivot動態PivotItems,我需要將帶有TextBlock的StackPanels的LongListSelector放入PivotItem中。我在TextBlock中綁定文本時遇到問題。綁定PivotHeader工作正常,但我不知道如何綁定名稱與TextBlock。如何將數據綁定到動態LongListSelector

XAML

<phone:Pivot x:Name="ShelfsPivot"> 
      <phone:Pivot.HeaderTemplate> 
       <DataTemplate> 
        <TextBlock Text="{Binding Header}"/> 
       </DataTemplate> 
      </phone:Pivot.HeaderTemplate> 
      <phone:Pivot.ItemTemplate> 
       <DataTemplate> 
        <phone:LongListSelector ItemsSource="{Binding LongListSelector}"> 
         <phone:LongListSelector.ItemTemplate> 
          <DataTemplate> 
           <StackPanel> 
            <CheckBox /> 
            <TextBlock Text="{Binding Name}" /> 
           </StackPanel> 
          </DataTemplate> 
         </phone:LongListSelector.ItemTemplate> 
        </phone:LongListSelector> 
       </DataTemplate> 
      </phone:Pivot.ItemTemplate> 
     </phone:Pivot> 

和代碼背後

public class LongListData 
{ 
    public string Name { get; set; } 
} 

public class PivotData 
{ 
    public string Header { get; set; } 

    public LongListSelector LongListSelector { get; set; } 
} 

public partial class MainPage 
{ 
    public MainPage() 
    { 
     InitializeComponent(); 

     var pivotDataList = new List<PivotData>(); 
     var pivotItem1 = new PivotData {Header = "Header1", LongListSelector = new LongListSelector()}; 
     var pivotItem2 = new PivotData {Header = "Header2", LongListSelector = new LongListSelector()}; 

     pivotDataList.Add(pivotItem1); 
     pivotDataList.Add(pivotItem2); 
     ShelfsPivot.ItemsSource = pivotDataList; 

     var list = new List<LongListData>(); 
     var item0 = new LongListData {Name = "Item1"}; 
     var item1 = new LongListData {Name = "Item2"}; 
     var item2 = new LongListData {Name = "Item3"}; 
     list.Add(item0); 
     list.Add(item1); 
     list.Add(item2); 

     foreach (var pivotItem in ShelfsPivot.Items) 
     { 
      var longlist = pivotItem as PivotData; 
      if (longlist != null) 
       longlist.LongListSelector.ItemsSource = list; 
     } 
    } 
} 
+0

檢查Visual Studio中的輸出窗口,看看是否有任何明顯的綁定錯誤? –

+0

您正在代碼中創建LongListSelector控件,並將它們綁定到另一個LongListSelector控件的ItemsSource屬性。這完全沒有意義。 – lsuarez

回答

1

該項目爲LongListSelector結合不應該是本身LongListSelector,它應該是一個列表或一個ObservableCollection。如果它是靜態數據,請使用List。如果它是動態數據,請使用ObservableCollection。

所以,它看起來應該與此類似。

public class LongListData 
{ 
    public string Name { get; set; } 
} 

public class PivotData 
{ 
    public string Header { get; set; } 

    public ObservableCollection<LongListData> ListData { get; set; } 
} 

public MainPage() 
{ 
    InitializeComponent(); 

    var list = new ObservableCollection<LongListData>(); 
    var item0 = new LongListData {Name = "Item1"}; 
    var item1 = new LongListData {Name = "Item2"}; 
    var item2 = new LongListData {Name = "Item3"}; 
    list.Add(item0); 
    list.Add(item1); 
    list.Add(item2); 

    var pivotDataList = new List<PivotData>(); 
    var pivotItem1 = new PivotData {Header = "Header1", ListData = list}; 
    var pivotItem2 = new PivotData {Header = "Header2", ListData = list}; 

    pivotDataList.Add(pivotItem1); 
    pivotDataList.Add(pivotItem2); 
    ShelfsPivot.ItemsSource = pivotDataList; 
} 

你的綁定看起來像這樣。

<phone:Pivot.ItemTemplate> 
    <DataTemplate> 
     <phone:LongListSelector ItemsSource="{Binding ListData}"> 
      <phone:LongListSelector.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <CheckBox /> 
         <TextBlock Text="{Binding Name}" /> 
        </StackPanel> 
       </DataTemplate> 
      </phone:LongListSelector.ItemTemplate> 
     </phone:LongListSelector> 
    </DataTemplate> 
</phone:Pivot.ItemTemplate> 

希望這有助於和快樂的編碼!

+0

感謝您的回答。它工作正常。 – user3333415

相關問題