簡體   English   中英

設置itemsource后,數據未顯示在datagrid中的Silverlight控件上

[英]Data not showing on Silverlight controls in datagrid after setting itemsource

我已經搜索了一段時間,但我沒有嘗試解決該問題。 下面的代碼執行沒有錯誤,但是模板中沒有數據顯示。

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

我已將最后一點更改了幾次。 任何幫助將不勝感激!

嘗試將您的Grid綁定到ObservableCollection 首先,我使用List,並且在加載DataGrid時遇到很多問題。 建議在Silverlight中使用ObservableCollection而不是List 為什么在Silverlight中使用ObservableCollection而不是List

using System.Collections.ObjectModel;

    ObservableCollection<Announcement> announcementCollection;

            public ObservableCollection<Announcement> AnnouncementCollection
            {
                get { return announcementCollection; }
                set
                {
                    announcementCollection = value;
                    NotifyPropertyChanged("AnnouncementCollection");
                }
            }

該代碼應該可以工作,盡管那里有一些多余的調用:

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

您應該檢查objAnnouncements.ToList()是否實際具有值。 在其上設置一個斷點。

檢查事項:

  1. 我想知道您的圖像是否掩蓋了網格。 嘗試先將其注釋掉。
  2. 我不知道什么是FieldSet。 當Datagrid在字段集之外時,它能工作嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM