嗨我有一個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;
}
}
}
檢查Visual Studio中的輸出窗口,看看是否有任何明顯的綁定錯誤? –
您正在代碼中創建LongListSelector控件,並將它們綁定到另一個LongListSelector控件的ItemsSource屬性。這完全沒有意義。 – lsuarez