wpf ItemsControl绑定问题

编程入门 行业动态 更新时间:2024-10-28 15:19:43
本文介绍了wpf ItemsControl绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在我的代码后面绑定到ItemsControl:

I bind to a ItemsControl in my codebehind:

ColumnVisibilityItems.DataContext = gc.ColumnVisibility;

其中ColumnVisibility是ObservableCollection,也使用字典对其进行了尝试.

where ColumnVisibility is a ObservableCollection, also tried it with dictionary..

我的标记

<ItemsControl x:Name="ColumnVisibilityItems"> <Label Content="{Binding Path=Name}" /> </ItemsControl>

单步执行过程中,我看到集合绑定中包含11个项目.但是ItemsControl仅呈现集合中的第一个项目.

while stepping through, i see the collection bound having 11 items. but ItemsControl renders only the first item in collection.

是否需要设置ItemsSource属性才能使其正常工作?因为每当我尝试在后面的代码中设置它时,我都会得到一个例外,说项目无法修改,因为它们已经存在.

Is ItemsSource property necessary to be set for this to work? because whenever i try to set that in code behind, i get the exception saying items cannot be modified because they exist already.

推荐答案

基本上,您需要指定模板.有关完整示例,请参见 msdn文档

basically you need to specify your Template. See the msdn docs for a fuller example

<ItemsControl x:Name="ColumnVisibilityItems" ItemsSource="{Binding}> <ItemsControl.ItemTemplate> <DataTemplate> <Label Content="{Binding Path=Name}" /> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>

更多推荐

wpf ItemsControl绑定问题

本文发布于:2023-11-12 03:04:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   wpf   ItemsControl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!