2

I started using ComponentOne's C1FlexGrid for my WPF Caliburn.Micro application. I wonder how I can set with of the columns to the maximum contents width. I tried to make it Auto, but the width is still the same for all columns.

I applied AutoSizeColumns like this:

      protected override void OnViewAttached(object view, object context)
  {
      var View = (FirstDataEntryView)view;
      View.EnrollmentFiles.AutoSizeColumns(0, 4, 5);
  }

but this didn't help...

Thanks

Nilay Vishwakarma
  • 3,105
  • 1
  • 27
  • 48
David Shochet
  • 5,035
  • 11
  • 57
  • 105

1 Answers1

1

Using GridLength.Auto usually doesn't work for these column widths because of virtualization. FlexGrid has a couple methods to autosize the columns but the columns must be visible or they won't be calculated and will remain the static width. Here is a very non-production-worthy example of how to do this. FWIW this is why I never liked ComponentOne controls -- it felt like they were stuck in WinForms mode.

edit: Adding some code for CM-specific usages.

protected override void OnViewAttached(object view, object context) {
    View = (YourViewType)view;
    View.flexGrid.AutoSizeColumns(0,4,5);
}

As far as whether or not accessing the view from a viewmodel is "compliant" with MVVM theories -- technically it is not, but sometimes you are forced to use controls that just cannot be automatically configured through XAML, and you can't use the View's code-behind because of one reason or another.

erodewald
  • 1,815
  • 20
  • 45
  • Thank you for your answer. But as I use Caliburn.Micro (without code-behind), is it possible to do it in xaml? – David Shochet Aug 08 '12 at 18:17
  • Although it deviates slightly from the loosely-coupled MVVM pattern, you can still access your View from your ViewModel. CM supports this by passing in the View object in OnViewAttached or OnViewLoaded. Override one of these [which best suits your needs] and capture that instance of your View. Then you can access your FlexGrid by name. I will edit this response with some code. – erodewald Aug 08 '12 at 18:21
  • Skipped over your actual question in that comment -- it does not appear to be possible according to their documentation and rumblings on the internet. This is a fairly common issue with datagrids unfortunately. – erodewald Aug 08 '12 at 18:30
  • I tried it (see my updated question), but because of some reason it didn't work. I wonder why... – David Shochet Aug 08 '12 at 18:43
  • @DavidShochet Try OnViewLoaded, the columns might not be visible yet (a requirement for this method to work). – erodewald Aug 08 '12 at 18:45
  • Hmmm... It says there is no suitable method to override... OnViewLoaded takes no parameters... so I cannot access the view. – David Shochet Aug 08 '12 at 18:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/15083/discussion-between-david-shochet-and-erode) – David Shochet Aug 08 '12 at 18:58