首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用WrapPanel绑定ItemsControl?

用WrapPanel绑定ItemsControl?
EN

Stack Overflow用户
提问于 2012-04-12 04:04:13
回答 1查看 565关注 0票数 0

我已经将WrapPanel的ItemsControl设置为:

代码语言:javascript
复制
            <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>

我的数据是

代码语言:javascript
复制
    private class mark_item
    {
        public mark_item()
        {
            this.timestamp= "";
        }
        public string timestamp { get; set; }
    }

    private List<mark_item> marks;

    itemsMarks.ItemsSource = marks;

正确地创建了列表标记,WrapPanel实际上包含了列表中的项数,但是TextBlock没有设置它的文本属性。

我遗漏了什么?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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时,这个问题就消失了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10113093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档