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

时间:2022-07-21 19:40:38

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.

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

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.

当我对ScatterView进行数据绑定时,WPF会自动使用ScatterViewItems包装DataTemplate的内容。我想为(生成的)ScatterViewItem的ScatterManipulationCompleted事件附加一些事件处理程序,但无法弄清楚如何执行此操作。

Any help is much appreciated.

任何帮助深表感谢。

3 个解决方案

#1


2  

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

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

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

#2


2  

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

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

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

#3


0  

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 :-(

经常发生这种情况,我现在找到答案了。我一直在看这个过去20个小时左右,只是在发布问题后5分钟找到它:-(

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:

任何方式:我找到的解决方案,现在帮助我的是使用ScatterView的Loaded事件。在处理程序中,我有以下循环:

    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

阅读http://www.beacosta.com/blog/?p=7后,这一切都来找我

Hope this helps anybody else.

希望这可以帮助其他人。

Bye, Bart

再见,巴特

#1


2  

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

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

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

#2


2  

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

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

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

#3


0  

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 :-(

经常发生这种情况,我现在找到答案了。我一直在看这个过去20个小时左右,只是在发布问题后5分钟找到它:-(

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:

任何方式:我找到的解决方案,现在帮助我的是使用ScatterView的Loaded事件。在处理程序中,我有以下循环:

    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

阅读http://www.beacosta.com/blog/?p=7后,这一切都来找我

Hope this helps anybody else.

希望这可以帮助其他人。

Bye, Bart

再见,巴特