0

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.

Palle Due
  • 5,929
  • 4
  • 17
  • 32
Findas
  • 69
  • 3
  • The WPF layout system shoudl do this automatically. You should include some more of your XAML so we can see what's around your `StackPanel`. – Keith Stein Apr 17 '20 at 22:01
  • Usually you don't need to touch on Measure and Arrange methods. Maybe you can use `ListBox` or `ListView` and set a source collection to their ItemsSource property. – emoacht Apr 29 '20 at 13:58

0 Answers0