Basic Library for WPF and Silverlight | ComponentOne
WPF and Silverlight Edition Basic Library / TreeView / TreeView Features / Node Selection
In This Topic
    Node Selection
    In This Topic

    When you click on a node at run time it is automatically marked as selected. Clicking a node will raise the SelectionChanged event to provide custom functionality. To have the nodes marked as selected without clicking them you can enable the IsSelected property.

    When the user selects a new item, the C1TreeView fires the SelectionChanged event. You can then retrieve the item that was selected using the SelectedItem property.

    There are several ways to do this. Two are discussed in the following content.

    You can also get the Text or Value of the C1TreeViewItem Header property using the SelectedItem property.

    Selecting Items with the Tag Property

    One is to assign additional data to the Tag property of each C1TreeViewItem as you create them. Later, you can inspect the Tag property to retrieve the information. For example:

    Visual Basic
    Copy Code
    ' Create a node and assign some data to its Tag property
    Dim item As New C1TreeViewItem()
    item.Header = "Beverages"
    item.Tag = beveragesID
    

     

    C#
    Copy Code
    // Create a node and assign some data to its Tag property
    C1TreeViewItem item = new C1TreeViewItem();
    item.Header = "Beverages";
    item.Tag = beveragesID;
    

    Later, use the information in whatever way you see fit:

    Visual Basic
    Copy Code
    Dim item As C1TreeViewItem = _tv.SelectedItem
    ' Handle beverages node
    If TypeOf item.Tag Is Integer AndAlso CInt(item.Tag) = beveragesID Then
    End If
    

     

    C#
    Copy Code
    C1TreeViewItem item = _tv.SelectedItem;
    if (item.Tag is int && (int)item.Tag == beveragesID)
    {
        // Handle beverages node
    }
    

    Selecting Items with a Checkbox

    You can also allow users to select C1TreeViewItems with a checkbox. To add a checkbox to your C1TreeViewItems, use the following markup:

    XAML
    Copy Code
    <c1:C1TreeView Name="C1TreeView1" Height="300" Width="200" >
        <c1:C1TreeViewItem IsExpanded="True" Margin="10">
            <c1:C1TreeViewItem.Header>
                <CheckBox>
                    <CheckBox.Content>
                        <TextBlock Text="Desktop" />
                    </CheckBox.Content>
                </CheckBox>
            </c1:C1TreeViewItem.Header>
        <c1:C1TreeViewItem>
            <c1:C1TreeViewItem.Header>
                <CheckBox>
                    <CheckBox.Content>
                        <TextBlock Text="User" />
                    </CheckBox.Content>
                </CheckBox>
            </c1:C1TreeViewItem.Header>
        </c1:C1TreeViewItem>   
    </c1:C1TreeView>
    

    Getting the Text or Value of a Selected Item

    The Header property will return the values contained in your C1TreeViewItems, you can get the string value using the following code:

    Visual Basic
    Copy Code
    Dim item As C1TreeViewItem = _tree.SelectedItem
    _textBlock.Text = item.Header.ToString()
    

     

    C#
    Copy Code
    C1TreeViewItem item = _tree.SelectedItem;
    _textBlock.Text = item.Header.ToString();