Patrick
Patrick

Reputation: 2593

C# wpf how to know a control (es. listbox) size when not specified

I have two borders in a winwdow, the first one contains a listbox which has size set through margins. So I can change the window size--->the border changes---> the listbox changes. I'd like to know what it's width is but the width property reports Nan while the actualWidth report 0.

<Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Border x:Name="Border1" BorderBrush="Gainsboro" BorderThickness="5" CornerRadius="8,8,3,3" Margin="10,10,168.8,10.4" >
                    <ListBox x:Name="lbButtons" Background="{x:Null}" BorderBrush="{x:Null}" Height="338" Margin="0,0,0.4,0"/>                        
                </Border>
                <Border x:Name="Border2" BorderBrush="Gainsboro" BorderThickness="5" CornerRadius="8,8,3,3" Grid.ColumnSpan="2" Margin="263,10,9.2,10" />
            </Grid>

This is something valid for each control: how to know its present size when it's not specified? thanx

Upvotes: 0

Views: 80

Answers (1)

Jean-Claude Colette
Jean-Claude Colette

Reputation: 937

It is possible that the component is not yet displayed at the point where you get its size. You can get the right actualWidth in the loaded event of the object.

Upvotes: 2

Related Questions