Silverlight 使用WrapPanel绑定ItemsControl?
我已使用WrapPanel将ItemsControl设置为:Silverlight 使用WrapPanel绑定ItemsControl?,silverlight,windows-phone-7,Silverlight,Windows Phone 7,我已使用WrapPanel将ItemsControl设置为: <ItemsControl Grid.Row="1" Height="200" Width="420" HorizontalAlignment="Center" Name="itemsMarks" VerticalAlignment="Top"> <ItemsControl.ItemsPanel> <ItemsPanelTem
<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;
private class标记\u项
{
公共标志项目()
{
这个。timestamp=“”;
}
公共字符串时间戳{get;set;}
}
私人名单标记;
itemsMarks.ItemsSource=标记;
列表标记已正确创建,WrapPanel实际上包含列表中的项目数,但TextBlock未设置其文本属性
我错过了什么
谢谢您需要将您的
标记项目
类别声明为公共
,而不是私人
Silverlight中的数据绑定只能访问public
类和属性。通过声明类private
,您将阻止Silverlight访问它
我接受了您的代码,看到了与您描述的相同的行为。ItemsControl
中出现了正确数量的项,但缺少文本。在VisualStudio/Visual Web Developer Express的输出窗口中,我还看到了以下消息。(由于消息本身足够长,我省略了stacktrace):
System.Windows.Data错误:无法从“PrivateClassProblem.MainPage+标记项”(类型“PrivateClassProblem.MainPage+标记项”)获取“timestamp”值(类型“System.String”)。BindingExpression:Path='timestamp'DataItem='PrivateClassProblem.MainPage+mark_item'(HashCode=12905972);目标元素是“System.Windows.Controls.TextBlock”(名称=“”);目标属性为“Text”(类型为“System.String”)。。System.MethodAccessException:尝试通过方法“System.Windows.CLRPropertyListener.get_Value()”访问方法“PrivateClassProblem.MainPage+标记_项。get_时间戳()”失败
当我声明类
public
时,问题就消失了。您没有在项控件
和列表数据之间创建正确的绑定。设置ItemsSource
属性是不够的。如果调试应用程序,您应该会在visual studio的输出窗口中看到一些绑定警告,是否可以发布其中一个?