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

  • SEARCH
  • Home
  • 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 7398339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:51:28+00:00 2026-05-29T03:51:28+00:00

Have youI know that the problem has been many times discussed in the web.

  • 0

Have youI know that the problem has been many times discussed in the web. But mine is a particular case and I still haven’t found the right solution.

Scenario: Silverlight 4 – A TreeView with data showed by two HierarchicalDataTemplate, one to show the first-level data (i.e. the data of the father-items of the TreeView), and one to show the second-level data (for the child-items). In the child-items template I have to bind the visibility of a control to a property of the data source class of the father template.

This is the XAML code:

<UserControl.Resources>
    <HierarchicalDataTemplate x:Key="modTreeArtDataParts2">
        <Grid>
            <TextBlock
                Visibility="{Binding ???}"/>
        </Grid>
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate x:Key="modTreeArtDataParts"
        ItemTemplate = "{StaticResource modTreeArtDataParts2}"
        ItemsSource = "{Binding RicambiItemList}">

    </HierarchicalDataTemplate>
</UserControl.Resources>

<Grid>
    <TreeView
        ItemTemplate = "{StaticResource modTreeArtDataParts}"
        ItemsSource="{Binding RicambiList}"/>
</Grid>

If it was WPF I could write:

Visibility = “{Binding DataContext.Ori, Converter={StaticResource rVisibilityConverter}, RelativeSource={RelativeSource AncestorLevel=2, AncestorType={x:Type TreeViewItem}, Mode=FindAncestor}}”

… and it would certainly works. But I know that in Silverlight FindAncestor as binding-mode with RealitiveSource is not supported. The solutions in the web are all around the scrolling down, in the code-behind, of the visual tree. It doesn’t matter if it’s realized with a Behavior or with an Attached-Propery. The solutions are like this:

Public Class hideTextBlockBehavior
    Inherits Behavior(Of DependencyObject)

    Protected Overrides Sub OnAttached()
        MyBase.OnAttached()
        Dim g As Grid = FindVisualParent(Of Grid)(AssociatedObject)
        Dim o As customType = g.DataContext
        If o.hide Then AssociatedObject.Visibility = Visibility.Collapsed
    End Sub

    Private Function FindVisualParent(Of parentItem As DependencyObject)(ByVal obj As DependencyObject) As parentItem
        Dim objParent As DependencyObject = obj
        While obj Is Nothing = False AndAlso TypeOf obj Is parentItem = False
            obj = VisualTreeHelper.GetParent(obj)
        End While
        Return DirectCast(obj, parentItem)
    End Function
End Class

<HierarchicalDataTemplate x:Key="modTreeArtDataParts2">
    <Grid>
        <TextBlock>
            <i:Interaction.Behaviors>
                <il:hideTextBlockBehavior/>
            </i:Interaction.Behaviors>
        </TextBlock>
    </Grid>
</HierarchicalDataTemplate>

I used many times solutions like this, and they always works. But in this case my DataTemplate is nested into another DataTemplate then, when I’m in the “OnAttached” method, the property “Parent” of the “AssociatedObject” is nothing, then I have no Visual-Tree to scroll.

Have you got a suggestion?
Thank you in advance!
Pileggi

  • 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-29T03:51:29+00:00Added an answer on May 29, 2026 at 3:51 am

    My suggestion would be to pass a reference to your parent view model to the children on construction:

    var itemVm = new ItemViewModel()
    {
        Description = "Parent", ChildVisibility = "Collapsed"
    }
    itemVm.Children = new List<ChildItemViewModel>
    {
        new ChildItemViewModel()
        {
            ParentVm = itemVm;
            Description = "Child"
        }
    }
    

    By doing so, you can easily bind to the property on the parent view model like so:

    <TextBlock Visibility="{Binding ParentVM.ChildVisibility}"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a n-tire web application and search often times out after 30 secs. How
I have written a small game in 'c' that has 15 Levels. I am
I've looked around for answers to this but have yet to find one that
I have been working on a upload file that works fine on localhost (windows)
I have been altering a query for a while now, reading many posts on
I challenge you :) I have a process that someone already implemented. I will
I'm using drupal 6.16 and have a set of taxonomy terms that are set
really sorry about being totally thick today but I have forgotten how to do
I have searched around and found some tutorials for xsl:key and key() function, but
I'm working on a research problem out of curiosity, and I don't know how

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.