开发者

Silverlight 3 ComboBox ItemTemplate binding

I have a simple ComboBox in my Silverlight 3 application. I want to populate it from an ObservableCollection. The list holds a class that has a Name(string), and a Selected(bool) property. The combo box has as many items as I have in the list, but I cannot seem to get the list data to appear.

Any help would be appreciated.

<ComboBox x:Name="cmbCategory" Grid.Column="3">
<ComboBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horiz开发者_高级运维ontal">
            <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;


I can't tell from your code if this is a Codebehind or a ViewModel. I am guessing that you are actually populating the _categories list in code so that it contains at least one cmbCategoryClass object. Try removing the line that sets the DataContext to _categories as your ItemsSource may be looking for a _categories property on the DataContext Check the Output window in Visual Studio when running in debug mode for clues to data binding failures.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜