Sunday, January 13, 2013

How to get the control inside datatemplate of a combobox in silverlight4

How to get the control inside datatemplate of a combobox in silverlight4

I have a checkbox inside the data template of combobox in silverlight4. Now i want to get the checkbox in combobox on selectedindex changed event of it.So how i can do that ?

Here is my code :

 <ComboBox Height="Auto" x:Name="CB_Categories" SelectionChanged="CB_Categories_SelectionChanged" Tag="">                                 <ComboBox.ItemTemplate>                                     <DataTemplate>                                     <CheckBox IsChecked="False" Content="{Binding Name}" CommandParameter="{Binding ProductCategoryID}" Click="CheckBox_Click" />                                     </DataTemplate>                                 </ComboBox.ItemTemplate>                             </ComboBox> 

Please Help me guys.

Thanks,

Answers & Comments...

Answer: 1

While it is possible to do so, you will probably be better off to just bind the checkbox property that you want to investigate or change, to a value in a background viewmodel or controller.

E.g. if it was the IsChecked that you want to change to true, then try this:

<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" Content="{Binding Description}" /> 

You then hook up the ComboBox to a list of items using its ItemsSource property:

<ComboBox ItemsSource="{Binding Options}">     <ComboBox.ItemTemplate>         <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" Content="{Binding Description}" />     </ComboBox.ItemTemplate> </ComboBox> 

Also you will have to set the DataContext of the container control or the ComboBox so that the control can access the object with those properties.

Doing it this way, means you can have less code in the code-behind of the control or window that is just doing plumbing work like updating controls.

If you need more examples, let me know...

by : Martin Lotteringhttp://stackoverflow.com/users/1308645




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog