2009-11-26 53 views
1

所有,绑定列表框在Silverilght不工作

名单上有什么我认为这是可能的数据在Silverlight中绑定的最简单的例子......但很显然,即使是太复杂,我:)

在XAML:

<UserControl x:Class="SilverlightApplication1.MainPage" 
    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:DesignWidth="640" d:DesignHeight="480"> 
<ListBox x:Name="rblSessions"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding SessionTitle}" Foreground="Black" FontSize="30" /> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

背后的代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 

namespace SilverlightApplication1 
{ 
    public partial class MainPage : UserControl 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 

      List<Sessions> theSessions = makeSessions(); 
      rblSessions.ItemsSource = theSessions; 
      rblSessions.DataContext = theSessions; 

     } 

     public List<Sessions> makeSessions() 
     { 
      List<Sessions> theReturn = new List<Sessions>(); 
      for (int i = 0; i < 20; i++) 
      { 
       Sessions s = new Sessions() { SessionID = i, SessionTitle = string.Format("title{0}", i) }; 
       theReturn.Add(s); 
      } 
      return theReturn; 
     } 

    } 

    public class Sessions 
    { 
     public int SessionID; 
     public string SessionTitle; 
    } 
} 

当我运行应用程序,我得到的是20元一个列表框,但每个元素都是空的,只有约5像素高(虽然我设置字号为“30”)

我在做什么错误?请帮助和感谢

/乔纳森

回答

1

你必须让你的会话类成员为特性,以便在结合使用它们。这应该解决它:

public class Sessions 
{ 
    public int SessionID { get; set; } 
    public string SessionTitle { get; set; } 
} 
+0

哦,我的!我是如何忽略这一点的?非常感谢* so * /jonathan – Jonathan 2009-11-26 19:05:01