• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How do I retrieve control runtime size values in C#?

W

WernerG

#1
Hello.

I am currently designing a WPF UI programmatically in C#. One of the custom
controls I am designing is a horizontal menu containing a set amount of
visually displayed buttons. The menu can contain more buttons that is
actually displayed and to get to them I have scroll buttons at the side.

Now, my Control that contains the buttons is a stackpanel with a horizontal
orientation. This stackpanel will be inserted into a grid that is chopped up
using GridUnitType.Star values.

My problem is that I cannot determine at run time what the size of my
stackpanel is. I do not programmatically set its size since it is set to fill
up the entire grid horisontally. ( Stretch ). I need the stackpanel's width
so that I can determine the correct size of my N buttons that I want to
insert into it. For example; my stackpanel's length is 80 and I want it to be
able to display 5 buttons so that give a button width of 80/5. I cannot get
that 80 at runtime.

Any help will be appreciated.

Regards,

Werner
 

My Computer

S

Serge Baltic

#2
Hello,

> For example; my stackpanel's
> length is 80 and I want it to be able to display 5 buttons so that
> give a button width of 80/5. I cannot get that 80 at runtime.
ActualWidth?..

Seems like the stack panel is not the right choice, as it's not for filling
the whole space with something. It's a grid-like behavior, rather. Will a
grid do instead, star-split for five buttons?

If you'd like to have five or six or else number of buttons, varied at runtime
based on the space available, then the best way is probably to create another
Panel-derived class for that. You'll have two method overrides there, one
for the Measure pass (what'd the desired size of your panel be — a sum of
all the button widths), and the Layout pass. On the latter, you are given
the room available (the length you'd like to get in runtime, that's it),
and you can decide how many buttons fit, and what their locations and widths
should be. If you cannot employ a Grid for that, a custom panel seems like
the best fit.

(H) Serge
 

My Computer

W

WernerG

#3
Interesting ideas.

In the mean time I have figured out that the values I was looking for are
located in FrameworkElement.ActualWidth/Height. Because those values are not
populated till the final stages of layout. The even SizeChanged fires when
the values are populated so I moved all my size calculation code to those
handlers.

Thanks for your help.

W

"Serge Baltic" wrote:

> Hello,
>

> > For example; my stackpanel's
> > length is 80 and I want it to be able to display 5 buttons so that
> > give a button width of 80/5. I cannot get that 80 at runtime.
>
> ActualWidth?..
>
> Seems like the stack panel is not the right choice, as it's not for filling
> the whole space with something. It's a grid-like behavior, rather. Will a
> grid do instead, star-split for five buttons?
>
> If you'd like to have five or six or else number of buttons, varied at runtime
> based on the space available, then the best way is probably to create another
> Panel-derived class for that. You'll have two method overrides there, one
> for the Measure pass (what'd the desired size of your panel be — a sum of
> all the button widths), and the Layout pass. On the latter, you are given
> the room available (the length you'd like to get in runtime, that's it),
> and you can decide how many buttons fit, and what their locations and widths
> should be. If you cannot employ a Grid for that, a custom panel seems like
> the best fit.
>
> (H) Serge
>
>
>
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)