[英]ItemsControl List<string>-Binding Won't Work
I used this as my template, but nothing shows in the Windows Phone Emulator. 我将其用作模板,但Windows Phone Emulator中没有任何显示。 I'm trying to bind a list of strings to an
ItemsControl
. 我正在尝试将字符串列表绑定到
ItemsControl
。 Won't work when I do it. 当我这样做时将无法正常工作。 I'm also trying to use a
StackPanel
, but I removed to try to get this to work. 我也在尝试使用
StackPanel
,但是为了StackPanel
,我删除了它。
PivotPage.xaml : 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 : 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(() => { })); }
}
To make it work Add this.DataContext=this;
要使其工作,请添加
this.DataContext=this;
after this.InitializeComponent();
之后
this.InitializeComponent();
in the constructor. 在构造函数中。
Why it is needed 为什么需要它
this.DataContext=this;
this.DataContext=this;
you are initializing the DataContext as the same class. ItemsSource="{Binding strings}"
inside your XAML. ItemsSource="{Binding strings}"
。 {Binding something}
has to be the same name of your property {Binding something}
,必须与您的媒体资源名称相同 The best way to do this is to Create a Class Model with your props 最好的方法是使用道具创建类模型
public class Model { public string Names{ get; set; } }
In your code behind you create a List
of your Class Model
在后面的代码中,创建类
Model
的List
public List<Model> model = new List<Model>();
Create a method to pupulate the fields. 创建一种方法来化字段。
public void ListModel() { model.Add(new Model { Names = "Hello" }); }
In your Main void you call ListModel();
在您的Main void中,调用
ListModel();
and PivotItem1.ItemsSource = model
. 和
PivotItem1.ItemsSource = model
。
Your XAML will be : 您的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.