问题描述
|
我是WPF的新手,以下内容使我感到困扰了一段时间:
我的模型中有一个ObservableCollection of People对象,该对象绑定到我的tabControl。因此,每添加一个新的People对象,就会创建一个新标签,其中People.Title作为Tab \的标题。
每个People对象都有一个ObservableCollection的Friend对象。在选项卡内部,我希望有两个文本框的列表,一个用于Friend.FirstName,另一个用于Friend.LastName。
我的第一个要求工作正常,但是第二个要求是给我一个错误“ ItemsSource已在使用中”
到目前为止,这是我的代码:
<TabControl Name=\"ConversationTabs\" Grid.Row=\"0\"
ItemsSource=\"{Binding}\"
ItemTemplate=\"{StaticResource HeaderInfoTabControl}\"
ContentTemplate=\"{StaticResource DialogueList}\" />
<Window.Resources>
<DataTemplate x:Key=\"HeaderInfoTabControl\">
<TextBlock Text=\"{Binding Title}\" />
</DataTemplate>
<DataTemplate x:Key=\"DialogueList\">
<ItemsControl ItemsSource=\"{Binding Path=DialogueCollectionVM}\">
<StackPanel Orientation=\"Horizontal\">
<TextBlock Text=\"{Binding Path=Sent}\" />
<TextBlock Text=\"{Binding Path=DateSent}\" />
<TextBlock Text=\"{Binding Path=Message}\" />
</StackPanel>
</ItemsControl>
</DataTemplate>
</Window.Resources>
我感谢您的帮助。解决方法
您不能将项目添加到ItemsControl并同时使用自动填充(通过ItemsSource)。如果应该将StackPanel用于ItemsSource中的项目,则应执行以下操作:
<ItemsControl ItemsSource=\"{Binding Path=DialogueCollectionVM}\">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation=\"Horizontal\">
<TextBlock Text=\"{Binding Path=Sent}\" />
<TextBlock Text=\"{Binding Path=DateSent}\" />
<TextBlock Text=\"{Binding Path=Message}\" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>