cannot retrieve object of TreeViewItem
I have a tree view in silver light which i am creating dynamically from my code behind on the load event of my .xaml page. My treeview contains numerous treeviewitems.The header of my treeviewitem contains a stack panel. My stackpanel contains a check box as child. I have created an event handler for Unchecked event of the check box.
Now here is my problem. When the unchecked event of my check box is triggered i want to retrieve the object of treeviewitem that is consuming the check box. Here is the code snippet that shows how i am creating my treeviewitem objTreeviewItem = new TreeViewItem();
objStackPanel = new StackPanel();
objStackPanel.Orientation = Orientation.Horizontal;
objCheckBox = new CheckBox();
objCheckBox.Content = "Checkbox1";
objCheckBox.Unchecked += new RoutedEventHandler(objCheckBox_Unchecked);
objStackPanel.Children.Add(objCheckBox);
objTreeviewItem.Header = objStackPanel;
Here is the code snippet for unchecked event of the checkbox
void objCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
try
{
TreeViewItem objItem = (((e.OriginalSource) as CheckBox).Parent as StackPanel).Parent as Tre开发者_开发问答eViewItem;
}
catch (Exception ex)
{
}
}
The above statement in the try block is returning me a null value. Hence i am not able to retrieve the treeviewitem which is consuming the check box on which event has been triggered.
So is there any other Property or method(other then the parent property) which can help me get the treeviewitem. Please any kind of help will be appreciatedThe OriginalSource
property does not return your CheckBox but whatever control on which the event started which may be a TextBlock inside the CheckBox, cast the sender
instead, it will always be the control the event is associated with.
精彩评论