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 8082035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:56:41+00:00 2026-06-05T16:56:41+00:00

In my application, we have a view model object from which we’re retrieving its

  • 0

In my application, we have a view model object from which we’re retrieving its attached FrameworkElement using our custom function GetVisualChildWithDataContext (see below).

What’s happening is that this function returns a ContentPresenter object with its ActualWidth and ActualHeight properties being 0. I would be expecting to get a value of 24 for the 2 properties since my content consists of a 24×24 rectangle.

My question is, how can I retrieve the values I’m expecting (ActualWidth and ActualHeight of 24) by only having a view model? If there’s a better way of doing this, I’m all ears.

Thanks for your time. (see sample below)

xaml:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l="clr-namespace:WpfApplication2"
        Title="MainWindow"
        Width="525"
        Height="350">
  <Canvas x:Name="_RootCanvas">
  <ItemsControl Margin="-5,0,0,0" ItemsSource="{Binding MyItems}">
    <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
        <Canvas />
      </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
      <DataTemplate DataType="{x:Type l:MyItem}">
        <Canvas>
          <Rectangle
              Canvas.Left="{Binding ActualLeft}"
              IsHitTestVisible="True"
              Margin="0,0,0,0"
              Width="24"
              Height="24"
              Fill="Black" />
        </Canvas>
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>

    <Button Canvas.Top="50" Width="100" Height="30" Click="Button_Clicked">
      <TextBlock Text="Button" />
    </Button>
  </Canvas>
</Window>

Code behind:

private MyItem _MyItem;
public MainWindow()
{
    InitializeComponent();

    _MyItem = new MyItem(0);
    MyItems = new ObservableCollection<MyItem>();
    MyItems.Add(_MyItem);

    DataContext = this;
}

public ObservableCollection<MyItem> MyItems { get; set; }

public void Button_Clicked(object sender, RoutedEventArgs e)
{
    FrameworkElement fe = GetVisualChildWithDataContext(_RootCanvas, _MyItem);
}

public static FrameworkElement GetVisualChildWithDataContext(DependencyObject parent, object dataContext)
{
    int count = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < count; i++)
    {
        DependencyObject d = VisualTreeHelper.GetChild(parent, i);
        FrameworkElement element = d as FrameworkElement;
        if (element != null)
        {
            if (element.DataContext == dataContext)
                return element;

            var f = GetVisualChildWithDataContext(element, dataContext);
            if (f != null)
                return f;
        }
    }

    return null;
}

public class MyItem
{
    public MyItem(double left)
    {
        ActualLeft = left;
    }
    public double ActualLeft { get; set; }
}
  • 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-06-05T16:56:44+00:00Added an answer on June 5, 2026 at 4:56 pm

    To fix my issue I created the following extension method:

    public static Size GetActualSize(this FrameworkElement parent)
    {
        var contentPresenter = parent as ContentPresenter;
        if (contentPresenter != null)
        {
            Rect descendantBounds = VisualTreeHelper.GetDescendantBounds(contentPresenter);
            if (!descendantBounds.Size.IsEmpty)
                return descendantBounds.Size;
        }
    
        return new Size(parent.ActualWidth, parent.ActualHeight);
    }
    

    Not pretty, but it gets the job done.

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

Sidebar

Related Questions

I have to implement a application which displays a grid view of thubmnails each
I have a WPF application with MVVM. Assuming object composition from the ViewModel down
In my WPF application I have a View that is given a ViewModel, and
I have an MVVM application. In one of the ViewModels is the 'FindFilesCommand' which
I have application where i have two view controllers my first view and second
In my application,i have text view with long text. i need text wrapping like
In an MVC3 application I have a view rendering two Ajax partial views: a
In my Backbone application I have a main view that shows previews of posts.
hai all, in my iphone application i have a login view with two text
I have an view-based application where the user can do a lot of customization

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.