Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3801392
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:02:35+00:00 2026-05-19T14:02:35+00:00

I have a question of how I can use the visual tree helper to

  • 0

I have a question of how I can use the visual tree helper to get an object that I need.
I have a user controll called DialogItemControll that I call from my main page like this:

DialogItemControll ivDialogWindow = new DialogItemControll()
            ivDialogWindow.ivSave.Click += new RoutedEventHandler(ivSave_Click);
            ivDialogWindow.Show();

And then I have the method ivSave_Click that gets called when I click the save button on my user controll. That method looks like:

 void ivSave_Click(object sender, RoutedEventArgs e)
    {
        var button = sender as Button;
        var firstStack = button.Parent as StackPanel;
        var secondStack = firstStack.Parent as StackPanel;
        TextBox te = secondStack.FindName("ivUserComment") as TextBox;}

This is where you can see my attempts to use the get parent and so on. Not so nicely done. So what I want is to get the whole object like:

var controll = ?? as DialogItemControll

My DialogItemControll looks like this :

 <C1:C1Window x:Class="DialogItemControll"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:C1="clr-namespace:C1.Silverlight;assembly=C1.Silverlight"
mc:Ignorable="d"
d:DesignHeight="418" d:DesignWidth="401">

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0" Margin="5,5,5,5">
        <TextBlock Name="ivHelpComment" FontSize="18">test</TextBlock>
        <TextBox Name="ivUserComment" BorderThickness="2,2,2,2" Height="170"></TextBox>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" >
            <Button Name="ivSave" HorizontalAlignment="Right" Height="22" Width="70" Margin="0,10,20,0" Click="ivSave_Click">Spara</Button>
            <Button Name="ivCancel" HorizontalAlignment="Right" Height="22" Width="70" Margin="0,10,20,0" Click="ivCancel_Click">Avbryt</Button>
        </StackPanel>
    </StackPanel>
</Grid>

C1 window is the same as user control, its just a third party control.
Please help me with how I should use the tree helper in a good way to get the whole object.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:02:36+00:00Added an answer on May 19, 2026 at 2:02 pm

    You should try Linq To VisualTree:

    http://www.scottlogic.co.uk/blog/colin/2010/03/linq-to-visual-tree/

    This allows you to query the visual tree using a Linq style API (It is actually very similar to Linq to XML). In your example, you want to find a Descendant of a specific type. You can do this as follows:

    var dialogItemControl = button.Descendants<DialogItemControll>()
                                  .Cast<DialogItemControll>()
                                  .Single();
    

    The first part of the query finds all descendants of the queried element of a given type (you can also use Linq to VisualTree to find children, peers etc…), the second part casts the returned list to the given type, finally because we know there is only one descendant of the given type, we can use Single() to extract the single item.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question that can I use AS400ConnectionPool in Spring if then pls
The short question is: How can I use Visual Studio to create/compile/run projects that
Have a rather simple question. Does anyone knows if i can use jparallax both
Reading this article http://support.microsoft.com/kb/813878 I have a question: Where can I get ipseccmd.exe for
I have a question concerning the class generation in Visual Studio 2008. I use
I have a question about combobox.I use visual c# and I want to insert
I have written a custom dialog (form) that I can use in a C#
I have a question how can I use wxSqlLite in my wxWidgets applications? I
From what I have seen so far, it seems that you can use both
Simple question. I use Visual studio and I've just installed subversion, how can i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.