2012-04-11 78 views
0

我已经把我的ItemsControl与WrapPanel为:用WrapPanel绑定ItemsControl?

  <ItemsControl Grid.Row="1" Height="200" Width="420" HorizontalAlignment="Center" Name="itemsMarks" VerticalAlignment="Top"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <toolkit:WrapPanel HorizontalAlignment="Center" VerticalAlignment="Center"/> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
        <Image Margin="1" 
           VerticalAlignment="Center" 
           Source="Images/markg.png" 
           Width="70" 
           Height="70" /> 
         <TextBlock TextWrapping="Wrap" Foreground="Black" Text="{Binding timestamp}" FontSize="14" HorizontalAlignment="Center" /> 
        </StackPanel> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 

我的数据是

private class mark_item 
    { 
     public mark_item() 
     { 
      this.timestamp= ""; 
     } 
     public string timestamp { get; set; } 
    } 

    private List<mark_item> marks; 

    itemsMarks.ItemsSource = marks; 

列表标记被正确地创建和WrapPanel实际上包含有列表中的项目数量,但TextBlock不会获取其Text属性集。

我错过了什么?

感谢

+0

您尚未在您的ItemsControl和您的列表数据之间创建适当的绑定。设置'ItemsSource'属性是不够的。 – Bernard 2012-04-11 20:08:08

+0

如果你调试你的应用程序,你应该会看到一些绑定警告出现在Visual Studio的输出窗口中,你能发布一个恶作剧吗? – gbanfill 2012-04-11 20:19:49

回答

1

你需要声明你mark_item类作为public,不private

Silverlight中的数据绑定只能访问public类和属性。通过声明类private,可以防止Silverlight访问它。

我把你的代码保持原样,我看到了你描述的相同的行为。正确数量的项目出现在ItemsControl中,但文本丢失。我还在Visual Studio/Visual Web Developer Express的“输出”窗口中看到以下消息。 (我省略堆栈跟踪的消息本身是足够长的时间):

System.Windows.Data Error: Cannot get 'timestamp' value (type 'System.String') from 'PrivateClassProblem.MainPage+mark_item' (type 'PrivateClassProblem.MainPage+mark_item'). BindingExpression: Path='timestamp' DataItem='PrivateClassProblem.MainPage+mark_item' (HashCode=12905972); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String').. System.MethodAccessException: Attempt by method 'System.Windows.CLRPropertyListener.get_Value()' to access method 'PrivateClassProblem.MainPage+mark_item.get_timestamp()' failed.

当我宣布类public,问题消失。

+0

哟,白痴:)谢谢。 – Marin 2012-04-12 16:12:06

相关问题