我已经将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没有设置它的文本属性。
我遗漏了什么?
谢谢
发布于 2012-04-12 05:36:32
您需要将mark_item类声明为public,而不是private。
Silverlight中的数据绑定只能访问public类和属性。通过声明类private,你可以阻止Silverlight访问它。
我接受了你的代码,我看到了你描述的相同的行为。ItemsControl中显示了正确数量的项目,但缺少文本。我还在Visual Studio/Visual Web Developer速成版的输出窗口中看到了以下消息。(我省略了堆栈跟踪,因为消息本身已经足够长了):
时间戳错误:无法从'PrivateClassProblem.MainPage+mark_item‘(类型为'PrivateClassProblem.MainPage+mark_item')获取'timestamp’值(类型为‘PrivateClassProblem.MainPage+mark_item’)。BindingExpression: Path='timestamp‘DataItem='PrivateClassProblem.MainPage+mark_item’(HashCode=12905972);目标元素为'System.Windows.Controls.TextBlock‘(名称=’‘);目标属性为'Text’(类型为'System.String')..'System.Windows.CLRPropertyListener.get_Value()‘方法访问'PrivateClassProblem.MainPage+mark_item.get_timestamp()’方法的System.MethodAccessException尝试失败。
当我声明类为public时,这个问题就消失了。
https://stackoverflow.com/questions/10113093
复制相似问题