Showing posts with label layout. Show all posts
Showing posts with label layout. Show all posts

Thursday, September 23, 2010

C# WPF Grid System

There are multiple "root" elements that you can use when designing a WPF application.

I want to talk about the "Grid" element.

This is pretty useful when you have a 'template' layout.  When you know where UI elements are going to be, and/or you are sure that your screen will be resized, you can use the 'Grid' element as your root.

The Grid element is very similar to the Table element in HTML.

A specific question I came across, was "Can I make one row a fixed size, while the other rows grow as the user changes the size of the form?".  This was an issue, because as the user changes the size of the form, the Grid cells, grow and shrink with the form.

Yes, you can set a specific row's height or a column's width.

There are 3 different ways you can set the height of a row.

row.Height = new GridLength(...

The three choices are:

    Pixel - fixed.  The value entered will be the amount of pixels exactly.
    Auto - this will make the row the size needed by the content.
    Star - takes as much as needed, or the amount in percent.

Just thought this was interesting.

Tuesday, August 3, 2010

C# Forms Stop Redraw/Paint

I've tried to stop the 'Paint' event on a C# WinForm without overriding the 'Paint' event.

I've done some research and found a lot of people have this problem, but those problems can be easily fixed using the 'DoubleBuffered' property found on the form itself.  The main problem fixed by 'DoubleBuffered' is that the form flickers.

This is not my problem.

I want to stop the 'Paint' event of a 'TreeView', while I expand all the nodes.  This is important when there are many nodes in the tree.  If you 'Paint' while expanding, the form becomes unresponsive and ugly.

The only solution that I can come up with, is to 'Hide()' the control, expand the nodes, then 'Show()' the control again.  I tried this with the 'Form' itself, but it didn't seem natural.  By performing the operation with the control, it's tough to tell if the control is redrawing, or the nodes are just being added.

While this is a 'hacky' solution, I don't know any other way.

BTW: SuspendLayout() => ResumeLayout() doesn't stop the 'Paint' event.