我使用this作为我的模板,但Windows Phone模拟器中没有显示任何内容。我正在尝试将字符串列表绑定到ItemsControl
。我做的时候不会工作。我也试图使用StackPanel
,但我删除了试图让它工作。
PivotPage.xaml :
<Page
x:Class="WinPhone8__Practice.PivotPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WinPhone8__Practice"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:data="using:WinPhone8__Practice.Data"
mc:Ignorable="d"
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Transitions>
<TransitionCollection>
<NavigationThemeTransition>
<NavigationThemeTransition.DefaultNavigationTransitionInfo>
<CommonNavigationTransitionInfo IsStaggeringEnabled="True"/>
</NavigationThemeTransition.DefaultNavigationTransitionInfo>
</NavigationThemeTransition>
</TransitionCollection>
</Page.Transitions>
<Grid>
<Pivot x:Uid="Pivot" Title="MY APPLICATION" x:Name="pivot" CommonNavigationTransitionInfo.IsStaggerElement="True">
<!--Pivot item one-->
<PivotItem
x:Uid="PivotItem1"
Margin="19,14.5,0,0"
CommonNavigationTransitionInfo.IsStaggerElement="True">
<!--Double line list with text wrapping-->
<ItemsControl ItemsSource="{Binding strings}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</PivotItem>
</Pivot>
</Grid>
</Page>
PivotPage.xaml.cs :
public sealed partial class PivotPage : Page
{
private readonly NavigationHelper navigationHelper;
public List<string> strings { get; private set; }
public PivotPage()
{
this.InitializeComponent();
strings = new List<string>() { "Yes", "No", "Maybe", "I don't know", "Can you repeat the question?" };
this.NavigationCacheMode = NavigationCacheMode.Required;
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
}
/// <summary>
/// Gets the <see cref="NavigationHelper"/> associated with this <see cref="Page"/>.
/// </summary>
public NavigationHelper NavigationHelper
{
get { return this.navigationHelper; }
}
private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
await DoNothing();
}
private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
{
// TODO: Save the unique state of the page here.
}
private Task DoNothing() { return new Task(new Action(() => { })); }
}
答案 0 :(得分:1)
使其有效在构造函数中this.DataContext=this;
之后添加this.InitializeComponent();
。
为何需要
this.DataContext=this;
,您将DataContext初始化为同一个类。答案 1 :(得分:0)
ItemsSource="{Binding strings}"
。{Binding something}
必须与您的财产名称相同时执行此操作的最佳方法是使用道具
创建班级模型 public class Model
{
public string Names{ get; set; }
}
在您的代码中,您创建了List
类Model
public List<Model> model = new List<Model>();
创建一个方法来填充字段。
public void ListModel()
{
model.Add(new Model
{
Names = "Hello"
});
}
在主空白处,您拨打ListModel();
和PivotItem1.ItemsSource = model
。
您的XAML将是:
<Pivot x:Uid="Pivot" Title="MY APPLICATION" x:Name="pivot" CommonNavigationTransitionInfo.IsStaggerElement="True">
<!--Pivot item one-->
<PivotItem
x:Uid="PivotItem1"
Margin="19,14.5,0,0"
ItemsSource={Biding}
CommonNavigationTransitionInfo.IsStaggerElement="True">
<!--Double line list with text wrapping-->
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Names}" Foreground = "Black"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</PivotItem>
</Pivot>