none
How to find control inside listbox ?

    Question

  • Hi all,

    I am having Treeview control inside a listbox and I am trying to add Nodes to the treeview control via textbox. Can some please tell me how will I find the treeview control inside the Listbox and also find the selected Node if Treeview has any items.

    I tried using VisualTreeHelper but its not working.

    Thanks in advance.

    Monday, July 20, 2009 8:10 AM

Answers

All replies

  • can you post your xaml snippet- it makes it easier to post a exact soltuion

    Monday, July 20, 2009 12:45 PM
  • Hi,

    Yeah, a small, minimal and relevant repro is needed here to be sure of what the question is.

    It sounds like you're generating TreeViews in ListBoxItems through ListBox databinding.
    And you need a way to get to the TreeViews.
    If that's the case please use the ListBox.ItemContainerGenerator.ContainerFromItem method to get generated ListBoxItems in the ListBox. Those ListBoxItems should have the TreeView nested in them.

    (ListBox.ItemContainerGenerator is a new property in Silverlight 3. In Silverlight 2 you should still use the VisualTreeHelper)

    Monday, July 20, 2009 5:47 PM
  • Hi,

    Thanks for the reply. My requirement is to add/remove items which are like Treeview structure. So Im using Treeview inside a listbox as later on I have to add the selected Node in Canvas which is placed on right hand side of the Listbox.

    xaml Code:

    <ListBox x:Name="lbFactorTypes" Padding="2,0,0,0" Width="165" Margin="0,2,2,2">

    <ListBox.ItemTemplate>

    <DataTemplate>

    <StackPanel x:Name="spFactorTypes">

    <Toolkit:TreeView x:Name="tvFactorTypes"></Toolkit:TreeView>

    </StackPanel>

    </DataTemplate>

    </ListBox.ItemTemplate>

    </ListBox>

    Can you please help me.

    Thanks.

     

     

    Tuesday, July 21, 2009 1:34 AM
  • Hi,

    I tried this:

    StackPanel sb = ListBox.ItemsControlFromItemContainer(lbFactorTypes).FindName("spFactorTypes") as StackPanel;

    but its not working. Giving runtime exception as "Object reference not set to an instance of an object"

    Can you please help me.

    Thanks.

    Tuesday, July 21, 2009 4:46 AM
  • Anyone ? Please help me.

    Tuesday, July 21, 2009 6:49 AM
  • Varsha, 

    Based on  my understanding, what you have is a DataTemplate and I believe it is not aware of the listbox and the listbox is only aware of the datatemplate and not any controls inside it.

    Kindly correct me if I am wrong.

    Thanks

    Jac

    Tuesday, July 21, 2009 7:53 AM
  • Thanks Jac. Actually same thing I have done in Silverlight 2 but with checkbox control and it works fine. Now I want to do it with Treeview control.

    Can you please help me ?

     Or tell me is there any other way that I can try.

    Thanks.

    Tuesday, July 21, 2009 8:03 AM
  • Write a recursion function to retrieve all TreeView controls by using VisualTreeHelper.GetChild...It works, not elegant though.

    Friday, July 24, 2009 3:20 AM
  • Hi,

    Thanks for the reply but the problem is that I'm not able to find the Treeview control which is inside the Listbox.

    Can you please tell me how can I find it?

    Here is my xaml code:

    <ListBox x:Name="lbFactorTypes" Padding="2,0,0,0" Width="165" Margin="0,2,2,2">

    <ListBox.ItemTemplate>

    <DataTemplate>

    <StackPanel x:Name="spFactorTypes">

    <Toolkit:TreeView x:Name="tvFactorTypes"></Toolkit:TreeView>

    </StackPanel>

    </DataTemplate>

    </ListBox.ItemTemplate>

    </ListBox>

    Please help me, Thanks Smile

    Friday, July 24, 2009 3:51 AM
  • try Like this

    Suppose You Have Xaml file like this

    <Grid x:Name="maingrid" Width="894" Height="576">

    <Grid.RowDefinitions>

    <RowDefinition Height="25.4" ></RowDefinition>

    <RowDefinition Height="25.4" ></RowDefinition>

    <RowDefinition Height="*"></RowDefinition>

    </Grid.RowDefinitions>

    <ListBox x:Name="lst" Grid.Row="2" Background="Aqua">

    <Button x:Name="b1" Content="Hello" ></Button>

    <Button x:Name="b2" Content="Welcome" ></Button>

    </ListBox>

     

    </Grid>

     

    Then In code File find Buttons Like This in page.xaml.cs

    Button bt = (Button)(lst.FindName("b1"));

    MessageBox.Show(bt.Content.ToString());

    In the same way u can find treeview and rest of the controls  in listview

     

    Friday, July 24, 2009 4:23 AM
  • try Like this

    Suppose You Have Xaml file like this

    <Grid x:Name="maingrid" Width="894" Height="576">

    <Grid.RowDefinitions>

    <RowDefinition Height="25.4" ></RowDefinition>

    <RowDefinition Height="25.4" ></RowDefinition>

    <RowDefinition Height="*"></RowDefinition>

    </Grid.RowDefinitions>

    <ListBox x:Name="lst" Grid.Row="2" Background="Aqua">

    <Button x:Name="b1" Content="Hello" ></Button>

    <Button x:Name="b2" Content="Welcome" ></Button>

    </ListBox>

     

    </Grid>

     

    Then In code File find Buttons Like This in page.xaml.cs

    Button bt = (Button)(lst.FindName("b1"));

    MessageBox.Show(bt.Content.ToString());

    In the same way u can find treeview and rest of the controls  in listview

    Mark As answer if satisfied

    Friday, July 24, 2009 4:23 AM
  • public void FindTreeView(DependencyObject d_obj)
            {
                if (d_obj == null)
                {
                    return;
                }
                else
                {
                    if (d_obj.GetType() == typeof(TreeView))
                    {
                        // found it...
                    }
                    else
                    {
                        int count = VisualTreeHelper.GetChildrenCount(d_obj);
                        for (int i = 0; i < count; i++)
                        {
                            FindTreeView(VisualTreeHelper.GetChild(d_obj, i));
                        }
                    }
                }
            }

    Pass your ListBox into the function. Please let me know if it works for you.

    Friday, July 24, 2009 4:25 AM
  • Hello varshavmane,

    please try the fillowing link here you can access different controls inside the data grid.

    :

     

    http://highoncoding.com/Articles/71_Accessing_Different_Controls_inside_the_Datagrid.aspx

    Friday, July 24, 2009 4:32 AM
  • Thanks HarjinderSingh9988, Silverlight Eric, deepbhullar but its not working.

    I dont know it not finding that control.

    Do you have any other solution ?

     

    Friday, July 24, 2009 5:20 AM
  • One Method that worked for me is to Combine DataBinding and Loaded Event Handler. I put the example here:

    http://blog.tzankov.com/2009/07/accessing-and-identifying-objects-in.html

    Tzanimir

    Friday, July 24, 2009 6:04 PM
  • Hi ttzankov,

    That article is really great, it worked for me. The only problem is that what I'm getting is while adding new Node to Treeview.

    For Example: If I have root Node and then I select that node add child node it. Its works fine but if I what to add one more Root Node then I am not ale to make the selected Node as unselected and so the Node gets add to the first Root Node, which I dont want.

    Here is the code:

    Dictionary<System.String, TreeView> TreeViewChildren = new Dictionary<string, TreeView>();

    List<String> ChildTreeViewNames = new List<string>();

    public Page()

    {

    InitializeComponent();

    ibtnAddPopulationFactorType.MouseLeftButtonDown +=
    new MouseButtonEventHandler(ibtnAddPopulationFactorType_MouseLeftButtonDown);

    ibtnDeletePopulationFactorType.MouseLeftButtonDown += new MouseButtonEventHandler(ibtnDeletePopulationFactorType_MouseLeftButtonDown);

    List<String> ChildTreeViewNames = new List<string> { "tvFactorTypes" };

    lbFactorTypes.ItemsSource = ChildTreeViewNames;

    }

    void ibtnAddPopulationFactorType_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

    {

    if (tbPopulationFactorType.Text != "")

    {

    if (TreeViewChildren["tvFactorTypes"].SelectedItem != null)

    {

    TreeViewItem newParent = (TreeViewItem)TreeViewChildren["tvFactorTypes"].SelectedItem;TreeViewItem ndChild = new TreeViewItem();

    ndChild.Header = tbPopulationFactorType.Text;

    newParent.Items.Add(ndChild);

    }

    else

    {

    TreeViewChildren[
    "tvFactorTypes"].Items.Add(new TreeViewItem() { Header = tbPopulationFactorType.Text });

    }

    }

    else

    {

    MessageBox.Show("Please Enter Population Factor Type Name.");

    }

    }

    private void TreeView_Loaded(object sender, RoutedEventArgs e)

    {

    TreeViewChildren.Add((System.
    String)((TreeView)sender).Name, (TreeView)sender);

    }

    Can you tell me how to add second root node?

    Thanks again. Smile

    Saturday, July 25, 2009 3:23 AM
  • Hi

    To allow the TreeView to de-select all the Nodes, add statically on the template an event handler for the MouseLeftButtonUp - i.e.

    MouseLeftButtonUp="TreeView_MouseUp"

     with the following handler:rivate void TreeView_MouseUp(object sender, MouseButtonEventArgs e)

    {

    if (sender is TreeView && ((TreeView)sender).SelectedItem is TreeViewItem)

    {

    if(e.OriginalSource is Border)

    ((TreeViewItem)((TreeView)sender).SelectedItem).IsSelected = false;

    }

    }

     

    Now when you click on the tree view , but not on any TreeView Item, the currently selected Node will be de-selected and you acn add further nodes to the root.

    I hope this helps.

     

    Tzanimir

    Saturday, July 25, 2009 5:30 AM
  • Thanks for the reply but its not working Sad

    Saturday, July 25, 2009 5:44 AM
  • Hi

    make sure your DataTemplate looks like this:

    <DataTemplate>

    <StackPanel>

    <Toolkit:TreeView x:Name="{Binding}" Loaded="TreeView_Loaded" MouseLeftButtonUp="TreeView_MouseUp"></Toolkit:TreeView>

    </StackPanel>

    </DataTemplate>

    And to test it click somewhere on the TreeView but outside any Nodes. The code I posted to you I have as running sample. If you need fuerhter help resolving this you need to be more specific what does not work.

    Tzanimir

    Saturday, July 25, 2009 6:01 AM
  • Hi,

    This is what my Listbox looks like:

    <ListBox x:Name="lbFactorTypes" Padding="2,0,0,0" Width="165" Margin="0,2,2,2">

    <ListBox.ItemTemplate>

    <DataTemplate>

    <StackPanel x:Name="spFactorTypes">

    <Toolkit:TreeView x:Name="{Binding}" Loaded="TreeView_Loaded" MouseLeftButtonUp="TreeView_MouseUp"></Toolkit:TreeView>

    </StackPanel>

    </DataTemplate>

    </ListBox.ItemTemplate>

    </ListBox>

    Is there anything wrong in it ?

    Saturday, July 25, 2009 6:06 AM
  • Hi your DataTemplate looks OK. I posted a bit more detailed description and some troubleshooting on the TreeView curent Item de-selection  here:

    http://blog.tzankov.com/2009/07/de-selecting-all-nodes-in-silverlight.html

    You still don't provide information what actually does not work.Thus it is difficult for any of us to say whats wrong.

    Tzanimir

    Saturday, July 25, 2009 2:56 PM
  • Thank you so much ttzankov. Its working fine now. Big Smile

    Tuesday, July 28, 2009 3:12 AM