附加事件?如何在运行时删除 WPF DataGrid 的 ScrollChanged 事件处理程序

发布于 2024-12-11 05:43:48 字数 663 浏览 0 评论 0原文

WPF 数据网格 类(不是 Windows 窗体 DataGrid!)可以设置为自动处理滚动,无需外部 ScrollViewer,并且可以为控件的内部滚动条注册事件处理程序通过这样编写 XAML:

<DataGrid ScrollViewer.ScrollChanged="dGrid_ScrollChanged"  />

如果我错了,请纠正我,但在这种情况下,内部 ScrollViewer 似乎是某种未记录的附加属性。 ScrollViewer 不是 DataGrid 的公共字段,您在 DataGrid 文档中找不到对 ScrollViewer 或 ScrollChanged 事件的引用。换句话说,简单地执行 myDataGrid.ScrollViewer.ScrollChanged += dGrid_ScrollChanged 是行不通的。

所以我的问题是,如何在运行时添加或删除此 ScrollChanged 事件的事件处理程序?我试图理解这里发生的事情,就像我试图解决问题一样,所以解释越多越好。

The WPF DataGrid class (Not the Windows Forms DataGrid!) can be set up to automatically handle scrolling without an external ScrollViewer and it's possible to register an event handler for the control's internal scrollbar by writing XAML like such:

<DataGrid ScrollViewer.ScrollChanged="dGrid_ScrollChanged"  />

Correct me if I'm wrong but in this case, the internal ScrollViewer appears to be some kind of undocumented attached property. ScrollViewer is not a public field of DataGrid and you will find no reference to either ScrollViewer or the ScrollChanged event in the DataGrid documentation. In other words simply doing myDataGrid.ScrollViewer.ScrollChanged += dGrid_ScrollChanged doesn't work.

So my question is, how does one go about adding or removing an event handler for this ScrollChanged event at runtime? I'm trying to understand what's going on here as much as I'm trying to solve the problem so the more explanation the better.

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

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

发布评论

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

评论(2

飞烟轻若梦 2024-12-18 05:43:48

尝试在后面的代码中像这样使用UIElement的AddHandler和RemoveHandler -

dg.AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(dg_ScrollChanged));
dg.RemoveHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(dg_ScrollChanged));

由于ScrollViewer不是dataGrid的依赖属性,因此您需要使用AddHandler进行挂钩。就像你不能像这样简单地设置 Grid.RowSpan dg.Grid.RowSpan = 2
您必须设置附加属性,例如 dg.SetValue(Grid.RowSpanProperty, 2)
您需要挂钩附加属性的事件也是如此。

Try using UIElement's AddHandler and RemoveHandler like this in your code behind -

dg.AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(dg_ScrollChanged));
dg.RemoveHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(dg_ScrollChanged));

Since ScrollViewer is not a Dependency Property of your dataGrid, you need to hook using AddHandler. Just like you can't set Grid.RowSpan simply like this dg.Grid.RowSpan = 2
You have to set Attach Properties like dg.SetValue(Grid.RowSpanProperty, 2)
Same goes with events you need to hook for attached properties.

遗忘曾经 2024-12-18 05:43:48

您可以使用 UIElement.RemoveHandler 方法。

如果您的网格有一个名称:“grid”,那么您可以这样做:

grid.RemoveHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(dGrid_ScrollChanged));

ScrollViewer.ScrollChanged 不是 DataGrid 的属性,但它是您可以在使用 ScrollViewer

You can use UIElement.RemoveHandler method.

if your grid has a name: "grid" then you can do it like this:

grid.RemoveHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(dGrid_ScrollChanged));

The ScrollViewer.ScrollChanged is not a property of DataGrid, but it's attached property that you can use on FrameworkElements that use ScrollViewer

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