views:

82

answers:

1

I got a WPF MVVM application containing a ListView containing a GridView.
Using a helper class I'm creating the GridViewColumn's, which works nicely.

My problem: I want to twoway-bind the width so I can read back changes to the width.

The code to create the GridViewColumn right now looks like this:

private static GridViewColumn CreateColumn(GridView gridView, object columnSource)
{
  GridViewColumn column = new GridViewColumn();
  String headerTextMember = GetHeaderTextMember(gridView);
  String displayMemberMember = GetDisplayMemberMember(gridView);
  String widthMember = GetWidthMember(gridView);
  // set header
  column.Header = GetPropertyValue(columnSource, headerTextMember);

  // set display binding
  String propertyName = GetPropertyValue(columnSource, displayMemberMember) as String;
  column.DisplayMemberBinding = new Binding(propertyName);

  // bind with - but how?
  //Binding widthBinding = new Binding(widthMember);
  //widthBinding.Source = columnSource;
  //widthBinding.Mode = BindingMode.TwoWay;
  //gridView.SetBinding(GridViewColumn.WidthProperty, widthBinding); <- gridView got no SetBinding :(
  }
  return column;
}

Anyone got some pointers for me how I might be able to bind the width?

+1  A: 

Check this out: BindingOperations.SetBinding

This method creates a new instance of a BindingExpressionBase and associates the instance with the given dependency property of the given object. This method is the way to attach a binding to an arbitrary DependencyObject that may not expose its own SetBinding method.

BindingOperations.SetBinding(column, GridViewColumn.WidthProperty, widthBinding);
Veer
the GridView does not contain the method SetBinding, where do I have to look for this?
Sam
It's not in GridView, it's in BindingOperations, as said in the answer...
Thomas Levesque
@Sam: Yes! Since the GridViewColumn class (not GridView) doesn't have the SetBinding method, the binding has to be done this way. Check my edit for code.
Veer
Oh, BindingOperations is a static class - I always was looking for some class derived from BindingOperations.Thanks!!
Sam