简体   繁体   中英

WPF Toolkit DataGrid column resize event

I am using WPF Toolkit Datagrid in one of the applications I am working on. What I want is to store the column width and displayindex as a user preference. I have achived it for column displayindex but for resize I could not find any event on the datagrid which will trigger after column size change. I have tried the "SizeChanged" event which I guess is only fired when it is initially calculating the size and that too is for the whole datagrid and not for the individual columns.
Any alternate solution or if anybody knows about the event ?

taken from... :

http://forums.silverlight.net/post/602788.aspx

after load :

    PropertyDescriptor pd = DependencyPropertyDescriptor
                             .FromProperty(DataGridColumn.ActualWidthProperty,
                                           typeof(DataGridColumn));

        foreach (DataGridColumn column in Columns) {
                //Add a listener for this column's width
                pd.AddValueChanged(column, 
                                   new EventHandler(ColumnWidthPropertyChanged));
        }

2 methods:

    private bool _columnWidthChanging;
    private bool _handlerAdded;
    private void ColumnWidthPropertyChanged(object sender, EventArgs e)
    {
        // listen for when the mouse is released
        _columnWidthChanging = true;
        if (!_handlerAdded && sender != null)
        {
            _handlerAdded = true;  /* only add this once */
            Mouse.AddPreviewMouseUpHandler(this, BaseDataGrid_MouseLeftButtonUp);
        }
    }

    void BaseDataGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (_columnWidthChanging) {
            _columnWidthChanging = false;
          // save settings, etc

        }

        if(_handlerAdded)  /* remove the handler we added earlier */
        {
             _handlerAdded = false;
             Mouse.RemovePreviewMouseUpHandler(this, BaseDataGrid_MouseLeftButtonUp);
        }
    }

The ColumnWidthPropertyChanged fires constantly while the user drags the width around. Adding the PreviewMouseUp handler lets you process when the user is done.

LayoutUpdated?

I'm working in Silverlight, and the grid is rebound/refreshed every second.

I'm using the LayoutUpdated method, which fires for every layout updating event.

You could keep a dictionary of the column widths and check for deltas. Then you would know which column(s) had changed.

foreach (DataGridColumn column in dataGrid1.Columns)
{
    // check for changes...
    // save the column.Width property to a dictionary...
}

You could try extending DataGrid and then implementing a NotifyPropertyChange event. Something like this:

class MyDataGrid : DataGrid, INotifyPropertyChanged
{
    private DataGridLength _columnWidth;
    public DataGridLength ColumnWidth
    {
        get { return _columnWidth; }
        set
        {
            _columnWidth = value;
            NotifyPropertyChanged("ColumnWidth");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

From there, you can add a delagate to the handler to do whatever you want it to do. Something like:

MyDataGrid dataGrid;
//init grid, do stuff

dataGrid.PropertyChanged += new PropertyChangedEventHandler(ColumnWidthChanged);
//ColumnWidthChanged will be a method you define

Now that you have the delagate, you can define what you want to happen when the column width is changed:

private void ColumnWidthChanged(object sender, PropertyChangedEventArgs args)
{
    if(args.PropertyName.Equals("ColumnWidth"))
    {
        //Do stuff now that the width is changed
    }
}

You'll notice that I'm checking for which property was changed. The way I set it up is such that you can extend other properties and make handlers for their change as well. If you want more then one handler, it would probably be best to make a DataGridPropertyChanged method that switches on which property was changed. It would then call the appropriate method (such as ColumnWidthChanged ) for each property that gets changed. That way, you wont have to be checking that each handler only modifies one property.

You didn't specify a language, so I re-tagged this to C#. However, it should be simple enough to transpose to VB if that's what you're using.

Hope this helps!

I assume you want to save the column widths so that the next time the application is started those same column widths are used to generate the data grid.

If that's the case then an alternative is to save the column widths (and indexes) when the application is closing, which would also be more efficient than saving the widths every time a column is resized.

Depending on how your application is structured, something like this should work...

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
  foreach (DataGridColumn column in dataGrid1.Columns)
  {
    // save the column.Width property to a user setting/file/registry/etc...
    // optionally save the displayindex as well...
  }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM