WPF UserControl绑定(bind)问题

标签 wpf silverlight

我有以下嵌入在另一个用户控件中的用户控件。

<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/

相关文章:

wpf - 如何在 XAML 中根据窗口或屏幕大小设置网格列最大宽度

c# - WPF 中的 XML 数据绑定(bind)

silverlight - ria 服务如何管理事务

silverlight - Silverlight socket

c# - 为什么有些人使用 `this.` ?

wpf - MVVM模式-如何在后面的代码中触发ICommand或CommandReference

.net - XAML 语法错误阻碍了设计器,但编译正常

c# - 使用 Silverlight 适当实现 IDisposable?

silverlight - Silverlight 中的样式触发器

silverlight - 如何在Silverlight 4中用NavigationService实现MVVM?