本文介绍了如何在ListBox的加载处理程序中获取ContentControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这样的活动中使用了列表框控件;

I am using listbox control in my activity like this;

< ListBox Height =" 19"的Horizo​​ntalAlignment = QUOT;左"余量= QUOT; 23,90,0,0"名称= QUOT; listBox1中" VerticalAlignment = QUOT;陀螺"宽度= QUOT; 319"装载= QUOT; listBox1_Loaded"&NBSP; />

<ListBox Height="19" HorizontalAlignment="Left" Margin="23,90,0,0" Name="listBox1" VerticalAlignment="Top" Width="319" Loaded="listBox1_Loaded"  />

并且在listbox加载的处理程序中,我试图获取ContentControl,以便通过使用它我可以访问列表框并填充它。但我总是得到contentcontrol null。如何获取contentcontrol?

and in listbox loaded handler, I am trying to get ContentControl so that by using it I could get access to listbox and fill it. But i always get contentcontrol null. How can I get contentcontrol?

下面给出了列表框的加载处理程序;

Loaded handler of listbox is given below;

private void listBox1_Loaded(object sender,RoutedEventArgs e)

    {

    System.Windows.Controls.ContentControl contentControl = e.Source as System.Windows.Controls.ContentControl;

    if(contentControl == null)

        System.Windows.Forms.MessageBox.Show(" Content control is null);
$


    System.Windows.Controls.ListBox inputIModelsListBox = Helper.GetNamedChild< System.Windows.Controls.ListBox>(contentControl.Parent," listbox1",5);



&NBSP;&NBSP;&NBSP; inputIModelsListBox.Items.Add(" firstvalue");



    }

private void listBox1_Loaded(object sender, RoutedEventArgs e)
    {
    System.Windows.Controls.ContentControl contentControl = e.Source as System.Windows.Controls.ContentControl;
    if (contentControl == null)
        System.Windows.Forms.MessageBox.Show("Content control is null");

    System.Windows.Controls.ListBox inputIModelsListBox = Helper.GetNamedChild<System.Windows.Controls.ListBox>(contentControl.Parent, "listbox1", 5);

    inputIModelsListBox.Items.Add("firstvalue");

    }

 

 

 

 

 

 

 

推荐答案

http://social.msdn.microsoft.com/Forums/en-US/vswpfdesigner/thread/8047a3ae -fedf-42fe-99d2-908fa2a2f31d

您是否有任何理由不使用DataBinding来填充列表框?在WPF和Silverlight中,这是填充List的最常用方式。

Is there any reason why you are not using DataBinding to fill the List Box? In WPF and Silverlight this is by far the most normal way of filling a List.

Mark


这篇关于如何在ListBox的加载处理程序中获取ContentControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:33