0

Ok so I have a GridView with a DataTemplate and the ItemSource set to Binding. The GridView is populated perfectly and I have no issues there.

My problem is as follows

  1. User taps on an image inside the DataTemplate
  2. A Flyout is displayed showing a simple ListView of the GridView items (works)
  3. User taps on a ListView item inside the flyout
  4. The GridView item matching the ListView item is then updated (works fine)
  5. A StoryBoard should be started (doesn't work)

The idea is that one player has attacked another player and I would like an animation to be started on the attacked player, but I can only access the attacking player using (sender).

The storyboard is defined within the GridViewItem DataTemplate, I can access this if that GridViewItem was tapped by using the sender and .Find() However I cannot use this I want to start a storyboard on a GridViewItem that hasn't been tapped.

I had a thought that maybe the Storyboard could be written inside the class used for the GridViewItem Bindings? How then would this be done?

Or maybe use a DataTrigger, but can this be used when the value I want to check against is and int being reduced?

Sam.Pain
  • 33
  • 1
  • 6
  • 3
    Can you give some image and your code to help us to know? – Vladimir Bolshakov Jul 21 '17 at 11:18
  • I have taken the dev in a different direction now, but I would like to know for future reference. Triggering a storyboard in a different object instance than the one being acted upon? The .FindName and .Resources wont work that I know of? – Sam.Pain Jul 21 '17 at 22:35

2 Answers2

0

Use .Resources["MyStoryboard"] as Storyboard to get the Storyboard which is defined inside the DataTemplate. Also, use the ContainerFromIndex to get the item from the index.

Here is a code sample

In XAML

<DataTemplate>
    <Grid Name="MyGrid">
        <Grid.Resources>
            <Storyboard x:Name="MyStoryboard"/>
        </Grid.Resources>
        ....
    </Grid>
</DataTemplate>

In Code Behind

private void StartStoryboard(int index)
{
    var MyGridViewItem = MyGridView.ContainerFromIndex(index);
    Storyboard MyStoryboard = MyGridViewItem.Resources["MyStoryboard"] as Storyboard;
    MyStoryboard.Begin();
}

Now you can use StartStoryboard(index) to start the Storyboard when ever you want.

Vijay Nirmal
  • 5,239
  • 4
  • 26
  • 59
  • Could you please explain how this can be used to answer my question - access a Storyboard in a GridViewItem that hasn't been tapped (is NOT the sender) ? – Sam.Pain Jul 21 '17 at 22:23
  • @Sam.Pain My answer will work correctly for your case. I have added my explanation. – Vijay Nirmal Jul 22 '17 at 04:34
  • Hi I don't need a storyboard when the item is loaded or added. Just in general so it could be the 3rd item out of 5. – Sam.Pain Jul 23 '17 at 08:26
  • Unless I stored each storyboard when it's loaded and then referenced it. Maybe have a class property defined and add the storyboard when it's loaded? – Sam.Pain Jul 23 '17 at 08:36
  • @Sam.Pain When do you want to start storyboard? and Which storyboard to be started? – Vijay Nirmal Jul 23 '17 at 10:33
  • Ok. Inside gridview there are 5 instances. User taps the 2nd item which shows a fly out user then taps an item in the fly out. This should start a storyboard on the 4th item in the gridview. For example – Sam.Pain Jul 23 '17 at 10:37
  • So you will have the Index and you want to animate the corresponding storyboard. Am I right? – Vijay Nirmal Jul 23 '17 at 10:44
  • Yeah that's right. Will the class need a storyboard property? – Sam.Pain Jul 23 '17 at 11:49
  • @Sam.Pain Change your selection to the corresponding index and try using the sender from the selection changed event to start storyboard. – Vijay Nirmal Jul 23 '17 at 12:13
0

How do I access a control inside a XAML DataTemplate?

the link above talks about a similar issue, using the FindControl function worked a charm

Sam.Pain
  • 33
  • 1
  • 6