2

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Bart Roozendaal
  • 815
  • 2
  • 9
  • 21

3 Answers3

2

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>
Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • Yep, way better than my solution :-| – Bart Roozendaal Apr 05 '09 at 13:58
  • 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 Apr 16 '09 at 05:35
  • 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 Apr 16 '09 at 07:24
2

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
  • 1,099
  • 6
  • 8
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 :-(

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
  • 815
  • 2
  • 9
  • 21
  • This only works if you never add an additional SVI to your SV. You should use the routed events approach outlined by Ben. – markti Apr 16 '09 at 05:33