项目不绑定到Pivot项目Windows Phone 8.1
本文关键字:项目 Phone Windows Pivot 绑定 | 更新日期: 2023-09-27 17:53:46
我无法将列表绑定到数据透视项。请指导我解决这个问题。
下面是我的设计部分(.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>
下面是我的代码后面(.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);
}
下面是我的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);
}
}
}
下面是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;
}
}
它不绑定到pivot项。请告诉我我错在哪里
我可以看到两个问题:
绑定到枢轴。ItemsSource,绑定应该在属性上,而不是在类字段上:
ItemsSource="{Binding Articles}
BindListToView方法应该将新列表设置在属性上,而不是在类的内部字段上,以便触发PropertyChanged
事件。
public void BindListToView(List<Article> articleList)
{
Articles = new ObservableCollection<Article>(articleList);
}