Silverlight 3 ComboBox ItemTemplate绑定

时间:2010-01-14 18:59:13

标签: silverlight data-binding

我的Silverlight 3应用程序中有一个简单的ComboBox。我想从ObservableCollection中填充它。该列表包含一个具有Name(字符串)和Selected(bool)属性的类。组合框中包含的项目与列表中的项目一样多,但我似乎无法显示列表数据。

任何帮助都将不胜感激。

<ComboBox x:Name="cmbCategory" Grid.Column="3">
<ComboBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Name}"/>
            <CheckBox IsChecked="{Binding Selected}"/>
        </StackPanel>
    </DataTemplate>
</ComboBox.ItemTemplate>

...
private class cmbCategoryClass
    {
        public string Name { get; set; }
        public bool Selected { get; set; }
    }
    private ObservableCollection<cmbCategoryClass> _categories;
....
cmbCategory.DataContext = _categories;
cmbCategory.ItemsSource = _categories;

1 个答案:

答案 0 :(得分:0)

我无法从您的代码中判断出这是Codebehind还是ViewModel。我猜你实际上是在代码中填充_categories列表,以便它至少包含一个cmbCategoryClass对象。尝试删除将DataContext设置为_categories的行,因为您的ItemsSource可能正在DataContext上查找_categories属性在调试模式下运行时检查Visual Studio中的“输出”窗口是否存在数据绑定失败的线索。

相关问题