English 中文(简体)
缩略语3.0 海关清单Box数据表
原标题:Silverlight 3.0 Custom ListBox DataTemplate has a checkbox, checked event not firing

清单Box的数据模板由XamlReader动态确定。 Load。 我正在通过利用视觉TreeHelper获得Box物体来检查事件。 GetChild. 这一事件没有发射

《刑法》

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

如何处理检查活动? 请帮助

最佳回答

删除项目图并添加以下代码:

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

这就确定了这一问题。

问题回答

你们需要理解为什么<代码>ItemTemplate是<代码>DataTemplate。 对于每个项目,清单箱需要显示,它将使用LoadContent()方法。 这为所述内容创造了一个新例子,包括在这种情况下建立一个新的检查箱。 所有这一切在被指定为清单BoxItem的内容时都与该项目有关。

本案的所有检查箱都是独立的物体。 你们已经创立了另一个独立案例,在实际调查中,在任何地方都未使用,并附上一个活动手。 清单上所列物品的检查箱中,无一能够分享这名手稿,因此,事件代码从来就不需要。





相关问题
Silverlight Rich text box control

Our team decided that we need our own custom Rich text box control for Silverlight app we are developing. We looked at existing controls mentioned at A good rich text control for Silverlight but ...

Silverlight ImageBrush not rendering (with Bing Map Control)

I m trying to add an image to a Pushpin instance from the Silverlight Bing Map Control, but I can t seem to get it to render (the pushpin renders fine). This is probably a general WPF question rather ...

Silverlight OpenFileDialog DoEvents equivalent

I m processing large files after they are selected by the user. My code looks like the following: if (FileDialog.ShowDialog() == true) { // process really big file } This freezes up the UI so ...

list of controls with templates in silverlight

Does anyone know where to find a list of controls that you can set the template on in Silverlight? I ve wasted several hours now trying to create control templates only to find that the control doesn ...

Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Silverlight 3 - FindName returns null

This looks a bug to me.. Using Silverlight 3 and i have a user control defined in XAML and trying to access the object during runtime returns a null. <Grid> <common:CommonGridEditPanel x:...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

热门标签