Silverlight 3.0自定义ListBox DataTemplate有一个复选框,checked事件不触发

发布于 2024-08-31 18:18:46 字数 1428 浏览 12 评论 0原文

ListBox 的数据模板由 XamlReader.Load 动态设置。我通过使用 VisualTreeHelper.GetChild 获取 CheckBox 对象来订阅 Checked 事件。该事件没有被触发

代码片段

    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;
    }

如何处理选中的事件?请帮忙

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆沫 2024-09-07 18:18:46

您需要了解 ItemTemplateDataTemplate 的原因。对于列表框需要显示的每个项目,它将调用 LoadContent() 方法。这将创建所描述内容的新实例,在本例中包括一个新复选框。当项目被指定为 ListBoxItem 的内容时,所有这些都将绑定到该项目。

在这种情况下,复选框的所有实例都是独立的对象。您所做的只是创建了另一个独立实例,该实例在实际 UI 中的任何地方都没有使用,并向其附加了一个事件处理程序。列表中项目的复选框都不会共享此处理程序,因此永远不会调用事件代码。

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.

零度° 2024-09-07 18:18:46

删除了 ItemTemplate 并添加了以下代码

                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()); };

这解决了问题

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文