views:

448

answers:

3

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.

+1  A: 

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>

HTH, Kent

Kent Boogaart
Yep, way better than my solution :-|
Bart Roozendaal
Why go through the trouble? Plus using EventSetters can severly limit your flexibility in storing your ItemContainerStyle as it must be stored as a resource in XAML that has code behind. Why not just use the routed events approach outlined by Ben?
markti
This approach allows you to keep event logic with the Style. Nothing stops you from having code-behind with your resource dictionaries. Besides that, it keeps the event declaration closer to the thing it belongs to, making the XAML easier to follow.
Kent Boogaart
A: 

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

Bart Roozendaal
This only works if you never add an additional SVI to your SV. You should use the routed events approach outlined by Ben.
markti
+2  A: 

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

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