WPF GridView: Create colums via code, how to distribute the columns proportional. - Community Credit Forums
in

    Community Credit Forums

WPF GridView: Create colums via code, how to distribute the columns proportional.

Last post 05-09-2008 5:57 PM by MikeBrown. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 03-13-2008 1:53 PM

    WPF GridView: Create colums via code, how to distribute the columns proportional.

    I create the colums of the GridView in code. How to distribute the columns proportionally over the available size? I don't know the available width at that moment in time (when creating the columns) and setting column.Width = double.NaN doesn't work.

    I would be very grateful if somebody can help...

    Filed under: , ,
  • 03-13-2008 3:28 PM In reply to

    Re: WPF GridView: Create colums via code, how to distribute the columns proportional.

    The GridView does not have the concept of proportional column distribution built in.

    There are a few ways to do this, but I have a question.

    If you are creating the columns at run-time and your know the width of the GridView, I assume that you don't know the width of the data you want to display?  Do you have any idea of the data that is displayed in your columns?  If you have some idea this would make this task very simple.

    If you do not have any idea of the data that will be displayed in the columns, one thing you could do is after returning the data but before data binding takes place, read through the first 10-50 rows, and get an average length of each column and then set the column widths of your GridView.  This would be very simple to implement.

    Another option is to use a 3rd party grid.  Exceed and Infragistics both offer free data grids for WPF.

    Hope this helps.

    Cheers,

    Karl

    Code Project MVP, Karl's Blog : Get Mole v4 Here also!

    Just a grain of sand on the worlds beaches.
  • 03-13-2008 4:12 PM In reply to

    • JDunagan
    • Top 25 Contributor
    • Joined on 07-16-2006
    • Naples, FL
    • Posts 33

    Re: WPF GridView: Create colums via code, how to distribute the columns proportional.

    Bear with me, this might even work, but it's a little complex...

    In regular ole ASP.Net 2.0, there's a thing MS built called the CSS-Friendly control adapters. Among the controls it adapts (rendering behavior) for, is the Gridview. In this case, the GridView is still a table, but in the CSS-Friendly control adapters version of it, those rows (trs) have classes instead of the inline styles. Once those have classes, you can target the individual tds within to have styled widths, and that means you can use percentage widths.

    Now all you have to know is the number of columns you need, since you can then divide into 100% to get a uniform percentage value for the style.

    Unfortunately, a css file does not take programmatic input, so in order to calculate those percentages dynamically on the fly, you have to bring the style into the aspx component where it can be programmatically altered.

    The CSS-Friendly control adapters are on Codeplex at codeplex.com/cssfriendly. Whether the WPF GridView can be adapted similarly is way above my experience.

    Good luck!

    John

    Naples .Net Developer Group
    http://www.naplesdot.net
  • 05-01-2008 9:06 AM In reply to

    • MikeBrown
    • Top 100 Contributor
    • Joined on 04-22-2008
    • Indianapolis, IN
    • Posts 3

    Re: WPF GridView: Create colums via code, how to distribute the columns proportional.

    Unfortunately, the built-in GridView very much breaks the lookless model of most WPF controls. I guess in a way it is to be expected because it provides a very specific look for the listview. However, in theory it could stand to be more stylable.

    There are two approaches you can take to create the functionality you need. It depends on which style of development you're more comfortable with. The first approach involves pretty much implementing what John described. There have been a few blog posts regarding extending control functionality using attached properties (including one by yours truly). Basically, the idea is to create an attached property that performs an operation, adds an event handler, applies a style, or anything that can be done to a control by hooking into the DependencyPropertyChangedHandler.

    Using a similar method, you can create an AutoSize attached property for a gridview that takes the GridView, looks at the GridViewColumn definitions and automatically sizes them to take an equal amount of width across the GridView. Going a step further, you can create an attached property for the GridColumns that the AutoSize property can use to create proportional sizes for the columns (similar to how the Grid panel uses star values to do the same).

    I might have time to create a sample to show exactly what I'm speaking of this weekend if you can wait that long.

  • 05-01-2008 9:41 AM In reply to

    Re: WPF GridView: Create colums via code, how to distribute the columns proportional.

    Mike,

    Wow, so nice to have you a member on Community Credit.  Have a great day!

     

    Cheers,

    Karl

    Code Project MVP, Karl's Blog : Get Mole v4 Here also!

    Just a grain of sand on the worlds beaches.
  • 05-01-2008 12:59 PM In reply to

    Re: WPF GridView: Create colums via code, how to distribute the columns proportional.

    KarlShifflett:

    Wow, so nice to have you a member on Community Credit.  Have a great day!

     - Couldn't have said it better myself. Big Smile

    Kindest regards,
      David Silverlight

    Support Community Credit by giving some love to StupidCubicle.com


  • 05-01-2008 1:18 PM In reply to

    • MikeBrown
    • Top 100 Contributor
    • Joined on 04-22-2008
    • Indianapolis, IN
    • Posts 3

    Re: WPF GridView: Create colums via code, how to distribute the columns proportional.

    Aww shux guys, you're making me feel so speshul!Embarrassed

  • 05-09-2008 5:57 PM In reply to

    • MikeBrown
    • Top 100 Contributor
    • Joined on 04-22-2008
    • Indianapolis, IN
    • Posts 3

    Re: WPF GridView: Create colums via code, how to distribute the columns proportional.

    Jaeilkem,

    I haven't forgotten about you. Although I wonder if you're still around at this point. I've written a blog post that shows how to use an Attached Property to set a max width value for a GridView column. Expanding the example to make proportional widths for columns shouldn't be much more difficult. I will sit down and put some time into it this weekend.

Page 1 of 1 (8 items)
Powered by Community Server (Commercial Edition), by Telligent Systems