WPF 工具包 DataGrid 列调整大小事件

发布于 2024-09-28 22:46:50 字数 211 浏览 0 评论 0原文

我正在我正在开发的应用程序之一中使用 WPF Toolkit Datagrid。我想要的是将列宽和显示索引存储为用户首选项。我已经为列显示索引实现了它,但对于调整大小,我在数据网格上找不到任何事件,该事件将在列大小更改后触发。 我尝试过“SizeChanged”事件,我想该事件仅在最初计算大小时才会触发,并且该事件也是针对整个数据网格而不是针对各个列。
有什么替代解决方案或者是否有人知道该事件?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

刘备忘录 2024-10-05 22:46:50

取自...:

http://forums.silverlight.net/post/602788.aspx< /a>

加载后:

    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 种方法:

    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);
        }
    }

当用户拖动宽度时,ColumnWidthPropertyChanged 会不断触发。添加 PreviewMouseUp 处理程序可以让您在用户完成操作时进行处理。

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.

べ繥欢鉨o。 2024-10-05 22:46:50

布局更新了吗?

我在 Silverlight 中工作,网格每秒都会反弹/刷新。

我正在使用 LayoutUpdated 方法,该方法会为每个布局更新事件触发。

您可以保留列宽的字典并检查增量。然后您就会知道哪些列已更改。

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

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...
}
梦开始←不甜 2024-10-05 22:46:50

您可以尝试扩展DataGrid,然后实现NotifyPropertyChange 事件。像这样的事情:

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));
        }
    }
}

从那里,您可以向处理程序添加一个延迟来执行您想要它执行的任何操作。类似于:

MyDataGrid dataGrid;
//init grid, do stuff

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

现在您有了延迟,您可以定义当列宽更改时您想要发生的情况:

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

您会注意到我正在检查哪个属性已更改。我设置它的方式是这样的,您可以扩展其他属性并为它们的更改创建处理程序。如果您需要多个处理程序,最好创建一个 DataGridPropertyChanged 方法来切换更改的属性。然后,它会为每个发生更改的属性调用适当的方法(例如ColumnWidthChanged)。这样,您就不必检查每个处理程序是否只修改一个属性。

您没有指定语言,所以我将其重新标记为 C#。但是,如果您正在使用 VB,那么它应该足够简单,可以转换为 VB。

希望这有帮助!

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!

抱着落日 2024-10-05 22:46:50

我假设您想要保存列宽,以便下次启动应用程序时使用相同的列宽来生成数据网格。

如果是这种情况,那么另一种方法是在应用程序关闭时保存列宽度(和索引),这也比每次调整列大小时保存宽度更有效。

根据您的应用程序的结构,类似的东西应该可以工作......

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...
  }
} 

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...
  }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文