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)
{
GridViewCol开发者_如何学Goumn 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?
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);
精彩评论