Findas
Findas

Reputation: 109

Get the ActualWidth of a user control

I want to populate a Vertical StackPanel with objects of another UserControl. All UserControls should be scaled by the widest one. The widest control should use the full available width.

ParentControl
<Grid x:Name="mainGrid" >
  <StackPanel x:Name="myStackPanel" />
</Grid>

private void OnMyStackPanel_Loaded(object sender, RoutedEventArgs e)
{
  myStackPanel.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  myStackPanel.Arrange(new Rect(0, 0, myStackPanel.DesiredSize.Width, myStackpanel.DesiredSize.Height));
  var width = myStackPanel.ActualWidth;
}

The problem is that the actual width after is 0. I have to add an element to the StackPanel. Is there a way to wrap this into something that asumes the available width so I can read that? Using a Dispatcher is not working. The ParentControl is nested into a Grid Cell of a View.

Upvotes: 0

Views: 61

Answers (0)

Related Questions