WPF中的ItemSource和DataContext有什么区别?

13

有人能告诉我WPF中Listview的Itemsource和DataContext之间的区别吗?请附上示例。

1个回答

16

项目源(必须实现IEnumerable接口)将被用于创建出现在列表中的项目列表。DataContext(可以是任何对象)是默认对象,用于绑定您为ListView上的其他属性指定的任何绑定。

public List<string> ItemsObject = new List<string>() { "Item1", "Item2", "Item3" };
public AnyObject DataContextObject = new AnyObject() { WidthValue = 23 }

<ListView
           ItemsSource="{Resource_of_ItemsObject}"
           DataContext="{Resource_of_DataContextObject}"
           Width="{Binding Path=WidthValue}"/>

将生成一个列表,其中包含“Item1”,“Item2”和“Item3”,并以宽度23显示。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接