0

我用 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 属性。

我错过了什么?

谢谢

4

1 回答 1

1

您需要将您的mark_item类声明为public,而不是private

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

我按原样接受了您的代码,并且看到了您描述的相同行为。中出现了正确数量的项目,ItemsControl但缺少文本。我还在 Visual Studio/Visual Web Developer Express 的输出窗口中看到了以下消息。(我省略了堆栈跟踪,因为消息本身足够长):

System.Windows.Data 错误:无法从“PrivateClassProblem.MainPage+mark_item”(“PrivateClassProblem.MainPage+mark_item”类型)获取“时间戳”值(“System.String”类型)。BindingExpression: Path='timestamp' DataItem='PrivateClassProblem.MainPage+mark_item' (HashCode=12905972); 目标元素是'System.Windows.Controls.TextBlock'(名称='');目标属性是“文本”(类型“System.String”)。 System.MethodAccessException:尝试通过方法“System.Windows.CLRPropertyListener.get_Value()”访问方法“PrivateClassProblem.MainPage+mark_item.get_timestamp()”失败。

当我宣布课程public时,问题就消失了。

于 2012-04-11T21:36:31.913 回答