Search code examples
wpfdatatemplatepixelsense

How can I attach event handlers to items generated using databinding in WPF?


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.


Solution

  • 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>