10

I'm using ListView with GridView. Is there GridViewColumn resize event?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Marat Faskhiev
  • 1,282
  • 6
  • 17
  • 37

5 Answers5

30

I will handle the PropertyChanged event instead. The PropertyChanged event is not seen in the Visual Studio intellisense, but you can trick it :)

 GridViewColumn column = ...
 ((System.ComponentModel.INotifyPropertyChanged)column).PropertyChanged += (sender, e) =>
 {
     if (e.PropertyName == "ActualWidth")
     {
         //do something here...
     }
 };
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
chenz
  • 730
  • 7
  • 13
  • Very nice (+1). See this for an implentation of this technique in an attached property: http://wpftoolbelt.codeplex.com/SourceControl/changeset/view/33225#457436. – Helge Klein Jan 04 '11 at 20:41
4

Although GridViewColumn does not appear to have a Resize event, you can bind to the ColumnWidth property.

You can verify this with sample XAML below - no code behind needed for this example. It binds only in one direction, from the column width to the text box, and when you resize you will see the textbox immediately update with the column width.

(This is just a simple example; if you want to pick up the resize in code I would create a class with a Width property so binding will work in both directions).

<StackPanel>
    <ListView>
        <ListView.View>
            <GridView>
                <GridViewColumn Width="{Binding ElementName=tbWidth1, Path=Text, Mode=OneWayToSource}"  />
                <GridViewColumn Width="{Binding ElementName=tbWidth2, Path=Text, Mode=OneWayToSource}"  />
            </GridView>
        </ListView.View>
        <ListViewItem>Item 1</ListViewItem>
        <ListViewItem>Item 2</ListViewItem>
    </ListView>
    <TextBox Name="tbWidth1" />
    <TextBox Name="tbWidth2" />
</StackPanel>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Edward
  • 8,028
  • 2
  • 36
  • 43
3

Have a look at MSDN DridViewColumn details. It does not appaer to have such an event, probably some workaround required, I am not sure though. have look here

Hope it helps.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Asad
  • 21,468
  • 17
  • 69
  • 94
1
private void ListView_Loaded( object sender, RoutedEventArgs e )
{
     // Add the handler to know when resizing a column is done
     ((ListView)sender).AddHandler( Thumb.DragCompletedEvent, new   DragCompletedEventHandler( ListViewHeader_DragCompleted ), true );
}

private void ListViewHeader_DragCompleted( object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e )
{
     ListView lv = sender as ListView;
    ... code handing the resize goes here ...
}

XAML:

<ListView Loaded="ListView_Loaded">
Bailey
  • 11
  • 2
0

Another approach: you can attach a change event handler to the GridViewColumn Width property:

PropertyDescriptor pd = DependencyPropertyDescriptor.FromProperty(
    GridViewColumn.WidthProperty, typeof(GridViewColumn));
GridView gv = (GridView)myListView.View;
foreach (GridViewColumn col in gv.Columns) {
    pd.AddValueChanged(col, ColumnWidthChanged);
}

...

private void ColumnWidthChanged(object sender, EventArgs e) { ... }

(Inspired by an answer here, for a similar question about DataGrid.)

fadden
  • 51,356
  • 5
  • 116
  • 166