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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
dimajCommented:
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
Snarf0001Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.