我一直在寻找,我尝试过的任何东西都没有解决这个问题。以下代码执行时没有错误,但模板中没有显示数据。
<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="GOReviewSL.UserControls.Announcements"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="75" d:DesignWidth="280" xmlns:my="clr-namespace:Local;assembly=Local">
<Grid x:Name="LayoutRoot" Background="White">
<my:Fieldset Height="Auto" HorizontalAlignment="Left" Name="fieldset1" VerticalAlignment="Top">
<StackPanel VerticalAlignment="Top" Orientation="Vertical">
<sdk:DataGrid x:Name="AnnouncementsGrid" ItemsSource="{Binding}">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel VerticalAlignment="Top" Orientation="Vertical">
<HyperlinkButton x:Name="AnnouncmentTitleLink" FontWeight="Bold" Content="{Binding Title}" Click="AnnouncmentTitleLink_Click" />
<TextBlock x:Name="AuthorText" Text="{Binding Author}" FontSize="10" FontStyle="Italic"/>
<TextBlock x:Name="AnnouncementText" Text="{Binding Title}"/>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</StackPanel>
</my:Fieldset>
<Image Source="/GOReviewSL;component/Images/announcements.png" Height="20" HorizontalAlignment="left" Margin="20,1,0,0" VerticalAlignment="Top"/>
</Grid>
公告类:
public class Announcement
{
public string Author { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public string ModifiedDate { get; set; }
public Announcement()
{
this.Author = string.Empty;
this.Title = string.Empty;
this.Text = string.Empty;
this.ModifiedDate = string.Empty;
}
public Announcement(string author, string title, string text, string modifiedDate)
{
this.Author = author;
this.Title = title;
this.Text = text;
this.ModifiedDate = modifiedDate;
}
}
我的绑定代码:
public Announcements()
{
InitializeComponent();
objController.ListAnnouncementsCompleted += new EventHandler<ListAnnouncementsCompletedEventArgs>(objController_ListAnnouncementsCompleted);
objController.ListAnnouncementsAsync();
}
void objController_ListAnnouncementsCompleted(object sender, ListAnnouncementsCompletedEventArgs e)
{
var objAnnouncements = from el in e.Result
select el;
AnnouncementsGrid.DataContext = objAnnouncements.ToList();
AnnouncementsGrid.ItemsSource = objAnnouncements.ToList();
}
我已经多次改变了最后一点。任何帮助将不胜感激!
答案 0 :(得分:1)
尝试将Grid
绑定到ObservableCollection
。首先我使用List并在加载DataGrid时遇到很多问题。建议在Silverlight中使用ObservableCollection
而不是List
。 Why to use ObservableCollection instead of List in Silverlight
using System.Collections.ObjectModel;
ObservableCollection<Announcement> announcementCollection;
public ObservableCollection<Announcement> AnnouncementCollection
{
get { return announcementCollection; }
set
{
announcementCollection = value;
NotifyPropertyChanged("AnnouncementCollection");
}
}
答案 1 :(得分:0)
代码应该可以工作,尽管那里有一些冗余调用:
// This is not necessary, and neither is ItemsSource="{Binding}"
//AnnouncementsGrid.DataContext = objAnnouncements.ToList();
AnnouncementsGrid.ItemsSource = objAnnouncements.ToList();
您应该检查objAnnouncements.ToList()实际上是否有值。在它上面设置一个断点。
要检查的事项: