【发布时间】:2012-02-23 09:47:47
【问题描述】:
首先,我的场景。我有一个带有 ObservableCollection<UserControl> 类型的公共属性 Items 的 ViewModel,其中包含我想使用 <ItemsControl ItemsSource="{Binding Items}" /> 显示的用户控件。
我使用Application.Current.TryFindResource("ControlKey") as UserControl 获取用户控件。
当每个资源都有不同的x:Key 时,一切正常。但是,当我向 Items 添加更多具有相同 x:Key 的 UserControls 时,即使所有 UserControls 都存在于 Items。
例如,我将项目添加到集合中,如下所示:
Items.Add(Application.Current.TryFindResource("Filter1") as UserControl);
Items.Add(Application.Current.TryFindResource("Filter1") as UserControl);
Items.Add(Application.Current.TryFindResource("Filter2") as UserControl);
ItemsControl 中仅显示两个控件,一个带有 x:Key“Filter1”,一个带有“Filter2”。 x:Key“Filter1”的第二个 UserControl 未显示。
我错过了什么?非常感谢。
【问题讨论】:
标签: c# wpf itemscontrol