我无法将列表绑定到数据透视表项。请指导我解决这个问题。
以下是我的设计部分(.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);
}
}
}
以下是课程文章
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;
}
}
它不会绑定到数据透视表项。请指导我错了什么
答案 0 :(得分:0)
我可以看到两个问题:
在绑定到 Pivot.ItemsSource 时,绑定应该在不在类字段上的属性上:
ItemsSource="{Binding Articles}
BindListToView 方法应该在属性上设置新列表,而不是在类的内部字段上设置以触发PropertyChanged
事件。
public void BindListToView(List<Article> articleList)
{
Articles = new ObservableCollection<Article>(articleList);
}