【问题标题】:Data not showing on Silverlight controls in datagrid after setting itemsource设置 itemsource 后数据未显示在数据网格中的 Silverlight 控件上
【发布时间】:2012-03-11 04:48:25
【问题描述】:

我已经搜索了一段时间,但我尝试过的都没有解决这个问题。下面的代码执行没有错误,但模板中没有数据显示。

<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();
    }

我已经改变了最后一点。任何帮助将不胜感激!

【问题讨论】:

  • 我也遇到了这个问题,只在网格中显示空白,虽然row.count是对的。

标签: silverlight binding datagrid


【解决方案1】:

尝试将您的Grid 绑定到ObservableCollection。首先,我使用 List 并且在加载 DataGrid 时遇到了很多问题。建议在 Silverlight 中使用 ObservableCollection 而不是 ListWhy 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");
                }
            }

【讨论】:

  • 谢谢。这很有帮助。数据在那里,但没有显示。这有助于解决这个问题。谢谢你和下面的评论帮助解决了这个问题。
【解决方案2】:

代码应该可以工作,虽然里面有一些多余的调用:

// This is not necessary, and neither is ItemsSource="{Binding}"
//AnnouncementsGrid.DataContext = objAnnouncements.ToList();
AnnouncementsGrid.ItemsSource = objAnnouncements.ToList();

您应该检查 objAnnouncements.ToList() 是否确实具有值。在上面设置断点。

检查事项:

  1. 我想知道您的图像是否覆盖了网格。尝试先将其注释掉。
  2. 我不知道 FieldSet 是什么。数据网格在字段集之外时是否工作?

【讨论】:

  • 部分内容被掩盖了。字段集有助于隐藏这一点。我能够删除字段集以找到它,并在调整位置后将其添加回来。 (字段集是一个类似于 HTML 字段集的对象)。谢谢你和上面的评论帮助解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-22
  • 1970-01-01
  • 2014-09-03
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多