如何将事件处理程序附加到使用 WPF 中的数据绑定生成的项目?

发布于 2024-07-16 05:34:22 字数 385 浏览 4 评论 0原文

我正在为 MS Surface 设备进行开发,并使用 ScatterView 来显示一些数据。 下面的场景可能也适合普通的 ListBox (和 ListBoxItems)。

当我对 ScatterView 进行数据绑定时,WPF 会自动使用 ScatterViewItems 包装 DataTemplate 的内容。 我想为(生成的)ScatterViewItemScatterManipulationCompleted 事件附加一些事件处理程序,但不知道如何做到这一点。

任何帮助深表感谢。

I am developing for a MS Surface unit and am using a ScatterView to display some data. The scenario below probably fits a normal ListBox (and ListBoxItems) too.

When I databind the ScatterView, WPF automatically wraps the contents of the DataTemplate with ScatterViewItems. I want to attach some event handers for the ScatterManipulationCompleted event of the (generated) ScatterViewItem, but can't figure out how to do that.

Any help is much appreciated.

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

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

发布评论

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

评论(3

苄①跕圉湢 2024-07-23 05:34:22

您可以在容器类型上设置 Style 并指定 EventSetter,如下所示:

<surface:ScatterView>
    <surface:ScatterView.ItemContainerStyle>
        <Style TargetType="{x:Type surface:ScatterViewItem}">
            <EventSetter Event="ScatterManipulationCompleted" Handler="myHandler"/>
        </Style>
    </surface:ScatterView.ItemContainerStyle>
</surface:ScatterView>

You can set a Style on the container type and specify an EventSetter like this:

<surface:ScatterView>
    <surface:ScatterView.ItemContainerStyle>
        <Style TargetType="{x:Type surface:ScatterViewItem}">
            <EventSetter Event="ScatterManipulationCompleted" Handler="myHandler"/>
        </Style>
    </surface:ScatterView.ItemContainerStyle>
</surface:ScatterView>
冷…雨湿花 2024-07-23 05:34:22

您应该利用路由事件。 您只需在 ScatterView 级别监听此事件即可。

        <surface:ScatterView surface:ScatterViewItem.ScatterManipulationCompleted="OnManipulationCompleted"/>

You should take advantage of routed events. You can just listen for this event at the ScatterView level.

        <surface:ScatterView surface:ScatterViewItem.ScatterManipulationCompleted="OnManipulationCompleted"/>
等往事风中吹 2024-07-23 05:34:22

正如经常发生的那样,我现在找到了答案。 我在过去 20 个小时左右的时间里一直在研究这个问题,只是在发布问题 5 分钟后才找到它:-(

无论如何:我找到的解决方案并且目前对我有帮助的是使用 ScatterView 的 Loaded 事件在处理程序中,我有以下循环:

    for (int i = 0; i < MiniBrowserContent.Items.Count; i++)
{
    ScatterViewItem svItem = (ScatterViewItem)(MiniBrowserContent.ItemContainerGenerator.ContainerFromIndex(i));
    svItem.ScatterManipulationCompleted += new ScatterManipulationCompletedEventHandler(svItem_ScatterManipulationCompleted);
}

在阅读 http:// /www.beacosta.com/blog/?p=7

他人有帮助。

希望这对其
巴特

As so often happens, I now found the/a answer. I've been looking at this for the last 20 hours or so, only to find it 5 minutes after posting the question :-(

Any way: the solution I found and which helps me for now is to use the Loaded event of the ScatterView. In the handler, I have the following loop:

    for (int i = 0; i < MiniBrowserContent.Items.Count; i++)
{
    ScatterViewItem svItem = (ScatterViewItem)(MiniBrowserContent.ItemContainerGenerator.ContainerFromIndex(i));
    svItem.ScatterManipulationCompleted += new ScatterManipulationCompletedEventHandler(svItem_ScatterManipulationCompleted);
}

It all came to me after reading http://www.beacosta.com/blog/?p=7

Hope this helps anybody else.

Bye,
Bart

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