views:

566

answers:

2

The datatemplate for the ListBox is set dynamically by XamlReader.Load. I am subscribing to Checked event by getting the CheckBox object using VisualTreeHelper.GetChild. This event is not getting fired

Code Snippet

    public void SetListBox()
    {
        lstBox.ItemTemplate =
        XamlReader.Load(@"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name=""DropDownTemplate""><Grid x:Name='RootElement'><CheckBox  x:Name='ChkList' Content='{Binding " + TextContent + "}' IsChecked='{Binding " + BindValue + ", Mode=TwoWay}'/></Grid></DataTemplate>") as DataTemplate;

        CheckBox  chkList = (CheckBox)GetChildObject((DependencyObject)_lstBox.ItemTemplate.LoadContent(), "ChkList");

        chkList.Checked += delegate { SetSelectedItemText(); };
    }

    public CheckBox GetChildObject(DependencyObject obj, string name) 
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            DependencyObject c = VisualTreeHelper.GetChild(obj, i);
            if (c.GetType().Equals(typeof(CheckBox)) && (String.IsNullOrEmpty(name) || ((FrameworkElement)c).Name == name))
            {
                return (CheckBox)c;
            }
            DependencyObject gc = GetChildObject(c, name);
            if (gc != null)
                return (CheckBox)gc;
        }
        return null;
    }

How to handle the checked event? Please help

+1  A: 

You need to understand the reason why ItemTemplate is a DataTemplate. For each item the the list box needs to display it will call the LoadContent() method. This create a new instance of the content described including, in this case, a new checkbox. All this then gets bound to the item when it is assigned as the Content of a ListBoxItem.

All the instances of checkbox in this case are independent objects. All you have done is created yet another independent instance which is not used anywhere in the actual UI and attached an event handler to it. None of the checkboxes for the items in the list will share this handler hence the event code is never called.

AnthonyWJones
Thanks for your reply. But how can I handle the checked event?
Bhaskar
A: 

Removed ItemTemplate and added the below code

                var checkBox = new CheckBox { DataContext = item };
                if (string.IsNullOrEmpty(TextContent)) checkBox.Content = item.ToString();
                else
                    checkBox.SetBinding(ContentControl.ContentProperty,
                                        new Binding(TextContent) { Mode = BindingMode.OneWay });
                if (!string.IsNullOrEmpty(BindValue))
                    checkBox.SetBinding(ToggleButton.IsCheckedProperty,
                                        new Binding(BindValue) { Mode = BindingMode.TwoWay });
                checkBox.SetBinding(IsEnabledProperty, new Binding("IsEnabled") { Mode = BindingMode.OneWay });
                checkBox.Checked += (sender, RoutedEventArgs) => { SetSelectedItemText(true, ((CheckBox)sender).GetValue(CheckBox.ContentProperty).ToString()); };
                checkBox.Unchecked += (sender, RoutedEventArgs) => { SetSelectedItemText(true, ((CheckBox)sender).GetValue(CheckBox.ContentProperty).ToString()); };

This fixed the issue

Bhaskar