blu*_*bit 3 data-binding wpf observablecollection mvvm
WPF中的数据绑定很棒,但是当你尝试使事情变得更复杂时,实现起来就非常困难.
我有一个对象集合,其中每个对象具有绑定到用户控件的可观察属性.
我想(理想情况下)只是在我的集合中添加一个新对象,并在我的表单上显示一个新的用户控件.事情是用户控件需要动态创建,所以每次我添加到集合时我可能必须手动创建一个新控件,设置绑定并将其添加到我的窗口.是否有更简单的MVVM样式绑定到这样的结构?
使用ItemsControl和aDataTemplate
<ItemsControl ItemsSource="{Binding YourCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<uc:YourUserControl />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)