C# WPF How to Scale a Groupbox to Fit Content.

Hey all,

ill upload a piccy if nessary but hopefully you will be able to follow.

 I have a WPF interface where the user can add textboxes/textblocks as they find nessary, all textboxes/textblocks are already created but start in a  Collapsed state until the User adds them and they become 'Visible'.

All these Collapsed elements are in Collapsed Group Boxes which are themselves in a single stackpanal.

One of these group boxes, in particular, has 8 Textboxes in it.  Its highly unlikely the end user will use all 8 at the same time, Most people however will use 2-4 of the textboxes.

when a user adds 2 or 4 textboxes the groupBox containing them also becomes visable, the problem is the Groupbox is massive, as its size caters for 8 textboxes rather than 2 or 4.

Is it possible to compact the groupbox so its size is relative to how many textboxes are added by the user So the groupbox would be smaller if 2 or 4 textboxes are added and full size if the user adds all 8 for example?

Many Thanks,

John




Bronco78thAsked:
Who is Participating?
 
Snarf0001Connect With a Mentor Commented:
The fantastic part of WPF is that you shouldn't have to do any of that...
What container control is inside the groupboxes?  If you dont' specify any height for the groupbox, have a stackpanel as the child of the groupbox, and have the textboxes as children of that, ie:

GroupBox
    StackPanel
        Textbox1
        Textbox2
        Textbox3

Then the groupbox should autosize itself to whatever controls are visible.
0
 
dimajCommented:
You could add some logic that would change the width of your group box based on how many fields you have visible. Just add an even handler for VisibileChanged (or something of that sort). Then, in your handler loop through all your fields and for every visible field, increment your counter. Once done with your loop change the width of the groupbox based on visibility counter.
0
 
Bronco78thAuthor Commented:
Bah,

Just made a complete noob of myself as I suddenly thought after posting

whats wrong with setting the Height property of the groupbox for when  each 2,4,6,8 textboxes are added? Simples!!

Ill leave the thread open as others may have other ways of doing it which might be good for future reference, will award point in the morning as clearly I need sleep.

Many thanks for the quick reply Dimaj, will award points etc in the morning :)

Regards,

John
0
 
dimajConnect With a Mentor Commented:
Anytime!

Actually, if I were to revise my first post, I'd say the following:
1) Create a global variable that would contain however many visible items you have
2) Every time you change item visibility, you increment or decrement your global
3) In the VisibleChanged event handler, change your group box's dimensions based on your global.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.