【问题标题】:How to assign value of the dataContext to ListBox control in silverlight?如何将 dataContext 的值分配给 Silverlight 中的 ListBox 控件?
【发布时间】:2026-01-22 07:45:02
【问题描述】:

您好,我的用户控件中有 contentControl。我正在将样式应用于由 TextBlock 和 ListBox 组成的 contentControl。我将 textBlock 的文本绑定到 CategoryName(来自控件的标记)。我想将类别的子项绑定到 listBox。我已将 ContentControl 的 dataContext 属性设置为子项 []。现在如何将这些子项绑定到资源中的列表框。

在用户控件的加载事件中 面板 pnl = 作为面板的发送者; 类别类别 = 面板。标记为类别; Items[] items = GetChildItemsByCategoryId(category.CategoryID); mainContent.DataContext = 项目;

【问题讨论】:

    标签: silverlight


    【解决方案1】:

    如果控件在 mainContent 的可视化树中,只需在代码或 XAML 中设置

    ItemsSource={绑定}

    【讨论】: