This is a migrated thread and some comments may be shown as answers.

Display selected value in ComboBox with Checkbox

2 Answers 726 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Prasanna
Top achievements
Rank 1
Prasanna asked on 22 Nov 2008, 05:44 AM
Hi,
I am creating a combobox in which I have placed check boxes which allows me multiple selection. I tried it in two ways:

1. I used a collection of objects of entities defined by me.
2. Created a datatable and then assigned it to the combobox.

In both the approaches I have same problem of showing the seleced items with check box selected in the combobox.

I have used follwoing code, which is showing me values correctly in the combobox but the problem occures when I want to show the selected item's check box selected. Can anyone suggest me a solution for this Or if you have any other better idea to implement the multiple selecte combobox then I will be greatful to you.
i have used :

<ComboBox  Name="cboBox1"
                              Style="{StaticResource ComboBox}" ItemsSource="{Binding}"> 
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                    <CheckBox Margin="3" Name="chkInsideCboBox1"
                                              DataContext="{Binding ElementName=BusinessEntityName, Path=BusinessEntityId}"
                                              IsChecked="{Binding Path=Default}"  Style="{StaticResource CheckBoxFilter}"></CheckBox>
                                    <TextBlock Text="{Binding Path=BusinessEntityNameField}" ></TextBlock>
                                </StackPanel>
                            </DataTemplate>
                        </ComboBox.ItemTemplate> 
                    </ComboBox>

Thanks in advance

2 Answers, 1 is accepted

Sort by
0
Prasanna
Top achievements
Rank 1
answered on 26 Nov 2008, 11:59 AM
Hi,
I got the solution.
XAML Code:

 

 

 

<ComboBox Name="cboBox1" Style="{StaticResource ComboBox}" ItemsSource="{Binding}" > 
 
<ComboBoxItem> 
 
<ListBox x:Name="lstInsideComboBox" ItemsSource="{Binding}">  
 
<ListBox.ItemTemplate> 
 
<DataTemplate> 
 
<CheckBox Name="chkInsideList" Click="chkInsideList_Checked" Content="{Binding Path=Content_To_Display}"   
 
 IsChecked="{Binding Path=ISSELECTED}"></CheckBox> 
 
</DataTemplate> 
 
</ListBox.ItemTemplate> 
 
</ListBox> 
 
</ComboBoxItem> 
 
</ComboBox> 
 
 

Now you have to assign the datacontext of your list inside the combo box.

 

lstInsideComboBox.DataContext = dtSourceForList

The datatable which you are assigning as DataContext to the list should have atleast two columns.
1. Column Content_To_Display : this will be content which you want to display in combo box.
2. Column ISSELECTED : this will be a column of bit value which will have either True or False. And on the basis of this value,
    check box will be checked.

You may improve the look of this combobox by applying some style.

Regards

 

 

0
Murray
Top achievements
Rank 2
answered on 04 Oct 2010, 01:15 PM
Hi Prasanna

Could you post the full code including the Click event?
Tags
General Discussions
Asked by
Prasanna
Top achievements
Rank 1
Answers by
Prasanna
Top achievements
Rank 1
Murray
Top achievements
Rank 2
Share this question
or