3

I'm trying to calculate StackPanel width, height (located in middle cell of the grid) before window showing (for example in window constructor). How it can be achieved?

<Window x:Class="WpfApplication2.TestWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TestWindow" Height="300" Width="300">
<Grid Name="grid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>

    <StackPanel Grid.Row="1" Grid.Column="1" Name="stackPanel"></StackPanel>

</Grid>

Measure for Window also as for stackPanel sets DesiredSize to {0;0}

public partial class TestWindow : Window
{
    public TestWindow()
    {
        InitializeComponent();

        this.Measure(new Size(this.Width, this.Height)); // -> this.DesiredSize = {0;0}

        ...

    }
}

EDIT1

The following works for FixedPage:

fixedPage.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); 
fixedPage.Arrange(new Rect(0, 0, fixedPage.DesiredSize.Width, fixedPage.DesiredSize.Height));

Then we can access stackPanel.ActualWidth and stackpanel.ActualHeight.

But for the Window, it does not works.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
user1897059
  • 183
  • 2
  • 7

1 Answers1

2

Try the Loaded event:

public TestWindow()
{
    InitializeComponent();
    this.Loaded += new RoutedEventHandler(TestWindow_Loaded);
}

void TestWindow_Loaded(object sender, RoutedEventArgs e)
{
    //this.DesiredSize shouldnt be {0,0} now
}

EDIT: Due to the fact, that a StackPanel already takes the maximum Size, even with no items in it, its SizeChanged event will only be fired if you add to much items, so you can use the SizeChanged event of your StackPanel like this:

private void spTest_SizeChanged(object sender, SizeChangedEventArgs e)
{
    if (!(e.PreviousSize.Height == 0 && e.PreviousSize.Width == 0)) //will also be fired after loading
    {
        //Create another Page
    }
}

EDIT2: Another possible solution:

public MainWindow()
{
    InitializeComponent();
    yourStackPanelName.Loaded += new RoutedEventHandler(yourStackPanelName_Loaded);
}

void yourStackPanelName_Loaded(object sender, RoutedEventArgs e)
{
    double height = ((StackPanel)sender).ActualHeight;
    double width = ((StackPanel)sender).ActualWidth;
}
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Florian Gl
  • 5,984
  • 2
  • 17
  • 30
  • It's suitable for Window but Window here is only for example. I need to place Grid into FixedPage and I must find the dimentions of the stackPanel as a content zone for this page - it helps to answer how many items I can put in it ... For each item I define Mesure and now it's height and then I reached end of page I need to create another Page and so on .... – user1897059 Dec 12 '12 at 09:15
  • Why do you need to set the StackPanel's height and width? A Stackpanel without items always takes the height and the width of its parents area, so you dont need to set these by code. – Florian Gl Dec 12 '12 at 10:20
  • 1
    I want GET height and width not SET and as asked befor I need this to find out posible items count in stackPanel. If I put more items in it they will be invisible in FixedPage – user1897059 Dec 12 '12 at 10:28
  • thank's but it's not suitable for me .... each time then you scroll DocumentViewer to FixedPage with Loaded event it will be executed ... – user1897059 Dec 12 '12 at 10:51
  • Maybe you should post more code of your programm, so that we know what's suitable for you. – Florian Gl Dec 12 '12 at 11:02
  • for FixedPage solution is: 1st run fixedPage.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); 2nd fixedPage.Arrange(new Rect(0, 0, fixedPage.DesiredSize.Width, fixedPage.DesiredSize.Height)); 3d access stackPanel.ActualWidth stackpanel.ActualHeight for WINDOW it does not works – user1897059 Dec 12 '12 at 11:50