我有以下嵌入在另一个用户控件中的用户控件。
<UserControl.Resources>
<DataTemplate x:Key="ContextsTemplate">
<Label Margin="10,0,20,0" Content="{Binding Name}"/>
</DataTemplate>
</UserControl.Resources>
<ItemsControl Name="Contexts"
Background="Transparent"
ItemsSource="{Binding}"
Margin="0,0,0,0"
VerticalAlignment="Center"
AlternationCount="2"
ItemTemplate="{StaticResource ContextsTemplate}">
</ItemsControl>
下面是嵌入另一个用户控件 (controlA) 的用户控件 (controlB) 的 XAML 代码。
<local:ContextContentsUserControl Height="30" Content="{Binding Contexts}"/>
controlA 在屏幕上显示为“(Collection)”,但由于某种原因没有在标签中显示集合文本中的每个项目。请帮忙。
最佳答案
问题出在这里:
Content="{Binding Contexts}"
你的意思是:
DataContext="{Binding Contexts}"
你得到“(集合)”而不是你为 controlA 定义的内容的原因是你在 XAML 中为 ControlA 定义的内容被你的集合替换了。 UserControl 的 XAML 文件的主体只是设置其 Content
属性:您在设置后替换了它。
关于WPF UserControl绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2421575/