[英]Items are not bind to the Pivot items Windows Phone 8.1
I cannot bind the list to the pivot items. 我无法将列表绑定到枢纽项目。 Please guide me to solve this.
请指导我解决这个问题。
Below is my design part (.xaml) 以下是我的设计部分(.xaml)
<Page.Resources>
<ViewModels:ArticleViewModel x:Key="ViewModel" />
<DataTemplate x:Key="headerTest">
<TextBlock Text="{Binding HeadLine}" FontSize="16"/>
</DataTemplate>
<DataTemplate x:Key="pivotTemplate">
<StackPanel>
<Grid>
<TextBlock Text="Test"></TextBlock>
<Image Source="{Binding ImageURL}" ></Image>
<Grid>
<Border VerticalAlignment="Bottom" Height="65" Background="Black" Opacity="0.5">
</Border>
<TextBlock Text="{Binding HeadLine}" VerticalAlignment="Bottom" Margin="5 0 0 15" TextWrapping="Wrap" FontSize="{ThemeResource ContentControlFontSize}" Foreground="White" FontWeight="Bold" />
</Grid>
</Grid>
</StackPanel>
</DataTemplate>
</Page.Resources>
<Page.BottomAppBar>
<CommandBar>
<CommandBar.PrimaryCommands>
<AppBarButton x:Uid="Share">
<AppBarButton.Icon>
<BitmapIcon UriSource="/Assets/Share.png"/>
</AppBarButton.Icon>
</AppBarButton>
<AppBarButton Icon="Favorite"/>
<AppBarButton Icon="Comment"></AppBarButton>
</CommandBar.PrimaryCommands>
</CommandBar>
</Page.BottomAppBar>
<Grid Background="Gray">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Image Source="Assets/Maalaimalar_logo.png" HorizontalAlignment="Center" Margin="1 5 0 0"></Image>
</Grid>
<ScrollViewer Grid.Row="1">
<Pivot DataContext="{StaticResource ViewModel}" x:Name="pivot"
HeaderTemplate="{StaticResource headerTest}"
ItemTemplate="{StaticResource pivotTemplate}" ItemsSource="{Binding articlesList}">
</Pivot>
</ScrollViewer>
</Grid>
</Grid>
Below is my code behind(.xaml.cs) 以下是我后面的代码(.xaml.cs)
protected override void OnNavigatedTo(NavigationEventArgs e)
{
data = e.Parameter as Feed;
List<Article> feedList = new List<Article>();
feedList = data.Articles;
var viewModel = pivot.DataContext as ArticleViewModel;
viewModel.BindListToView(feedList);
}
Below is my ArticleViewModel 下面是我的ArticleViewModel
public class ArticleViewModel : BindableBase
{
ObservableCollection<Article> articlesList = new ObservableCollection<Article>();
public ArticleViewModel()
{
}
public void BindListToView(List<Article> articleList)
{
articlesList = new ObservableCollection<Article>(articleList);
}
/// <summary>
///
/// </summary>
public ObservableCollection<Article> Articles
{
get { return this.articlesList; }
set
{
Set<ObservableCollection<Article>>(ref this.articlesList, value);
}
}
}
Below is the class Article 下面是班级文章
public class Article
{
/// <summary>
///
/// </summary>
public string Title { get; set; }
/// <summary>
///
/// </summary>
public string HeadLine { get; set; }
/// <summary>
///
/// </summary>
public string ImageURL { get; set; }
/// <summary>
///
/// </summary>
public string Abstract { get; set; }
/// <summary>
///
/// </summary>
public string ArticleDetail { get; set; }
/// <summary>
///
/// </summary>
public string ArticleId { get; set; }
/// <summary>
///
/// </summary>
public Article()
{
}
/// <summary>
///
/// </summary>
/// <param name="title"></param>
/// <param name="headline"></param>
/// <param name="imageURL"></param>
/// <param name="articleAbstract"></param>
/// <param name="articleDetail"></param>
public Article(string title, string headline, string imageURL, string articleAbstract, string articleDetail, string articleId)
{
Title = title;
HeadLine = headline;
ImageURL = imageURL;
Abstract = articleAbstract;
ArticleDetail = articleDetail;
ArticleId = articleId;
}
}
It doesn't bind to the pivot item. 它不绑定到枢轴项目。 Please guide me what i'm wrong
请指导我我错了
I can see 2 issues: 我可以看到2个问题:
On the Binding to Pivot.ItemsSource , the binding should be on the property not on the class field: 在Binding to Pivot.ItemsSource上 ,绑定应该在属性上,而不是在class字段上:
ItemsSource="{Binding Articles}
BindListToView method should set the new list on the property, not on the internal fields of the class in order to trigger the PropertyChanged
event. BindListToView方法应该在属性上设置新列表,而不是在类的内部字段上设置新列表,以触发
PropertyChanged
事件。
public void BindListToView(List<Article> articleList)
{
Articles = new ObservableCollection<Article>(articleList);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.