Skip to main content

Microsoft Silverlight

Answered Question ListBox and ItemTemplateRSS Feed

(0)

novy
novy

Member

Member

2 points

17 Posts

ListBox and ItemTemplate

 Here is some code

<ListBox x:Name="lstRaporty" SelectionChanged="lstRaporty_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding Path=IsChecked, Mode=TwoWay}" Content="{Binding Path=Name}" Click="CheckBox_Click" Margin="0,5,0,5" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

When I click checkbox on the list the only "Click" event is raised and I have no information which item belongs to clicked checkbox because "SelectionChanged" event is not raised.

silverbyte
silverbyte

Participant

Participant

1338 points

405 Posts

Answered Question

Re: ListBox and ItemTemplate

Suppose your bounded class object is called MyClass

 

void CheckBox_Click(sender, e)

{

      MyClass myObj  = (sender as CheckBox).DataContext as MyClass;

       string name =  myObj.Name;


 }

Please click on 'Mark as answer' near my comment if you feel I answered your question.

y_makram
y_makram

Contributor

Contributor

6172 points

1,233 Posts

Re: ListBox and ItemTemplate

 THis is because clicking on the CheckBox will not even select the item. I have written the following code as a workaround:

private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            sender.ToString();
            CheckBox c = sender as CheckBox;
            int index = 0;
            foreach (company item in lstRaporty.Items)
            {
                if (item.Name == c.Content.ToString())
                    lstRaporty.SelectedIndex = index;
                index++;
            }
        }

Thanks
Yasser Makram
http://www.silverlightrecipes.com
_____
Dont forget to click "Mark as Answer" on the post that helped you. If your question has not been answered, please post a followup question.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities