Home > DeveloperSection > Forums > Checkbox click copy texblocks in to a string list
ben reitman
ben reitman

Total Post:96

Points:676
Posted on    January-30-2014 11:20 PM

 C# C# 
Ratings:


 1 Reply(s)
 808  View(s)
Rate this:

In the HierarchicalDataTemplate, i got 2 textblocksand I want to copy the texts from the both. During the check-box click/check it'll update the text to a list.

The function is getting the text from one textblock.

How can i get text from both textblocks and update it to the list?

private List<string> selectedNames = new List<string>();

private void TreeView_Checked(object sender, RoutedEventArgs e)

{

    CheckBox chkBox = sender as CheckBox;

    StackPanel stackPanel = chkBox.Parent as StackPanel;

    TextBlock txtBlock = FindVisualChild<TextBlock>(stackPanel);

    bool isChecked = chkBox.IsChecked.HasValue ? chkBox.IsChecked.Value : false;

    if (isChecked)

    {

        selectedNames.Add(txtBlock.Text );            

    }

}

CheckBox get text function:

private static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject

{

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)

    {

        DependencyObject child = VisualTreeHelper.GetChild(obj, i);

        if (child != null && child is T)

            return (T)child;

        else

        {

            T childOfChild = FindVisualChild<T>(child);

            if (childOfChild != null)

                return childOfChild;

        }

    }

    return null;

}

WPF HierarchicalDataTemplate:

<StackPanel Orientation="Horizontal" >

<CheckBox Name="checkBoxTree"  Checked="TreeView_Checked" Unchecked="checkBoxTree_Unchecked"

          Margin="0,4,0,0"  Style="{DynamicResource CheckBoxStyle1}"/>

<TextBlock Text="{Binding XPath=@Name, Mode=TwoWay}" />

<TextBlock >                              

           <Hyperlink NavigateUri="{Binding XPath=@WebSite}" RequestNavigate="Hyperlink_RequestNavigate"> 

               <TextBlock Text="{Binding XPath=@WebSite}" />

           </Hyperlink>   

</TextBlock>

</StackPanel>



Pravesh Singh

Total Post:411

Points:2881
Posted on    January-30-2014 11:47 PM

Hi Ben,

Try This:

private void TreeView_Checked(object sender, RoutedEventArgs e)

{

    CheckBox chkBox = sender as CheckBox;

    StackPanel stackPanel = chkBox.Parent as StackPanel;

    TextBlock txtBlock = FindVisualChild<TextBlock>(stackPanel);

    Hyperlink hyperlink = FindVisualChild<Hyperlink>(stackPanel);

    TextBlock secondTextBlock = FindVisualChild<TextBlock>(hyperlink);

    bool isChecked = chkBox.IsChecked.HasValue ? chkBox.IsChecked.Value : false;

    if (isChecked)

    { 

        selectedNames.Add(txtBlock.Text);  

        selectedNames.Add(secondTextBlock.Text);          

    }      

}


Don't want to miss updates? Please click the below button!

Follow MindStick