0

I am using Silverlight 5 and in a view I have a Button that, when clicked, loads a particular state. This is done using an EventTrigger and the GoToStateAction markup like so:

<Button x:Name="..." Content="...">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <ei:GoToStateAction StateName="MyState"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

The above works swimmingly, but if I move the Button into a DataGridTemplateColumn it no longer works. Specifically, the app compiles without error and the DataGrid shows the button in a column, but when I click the button the state is not transitioned to.

<data:DataGrid ...>
    <data:DataGrid.Columns>
        <data:DataGridTemplateColumn>
            <data:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button x:Name="..." Content="...">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <ei:GoToStateAction StateName="MyState"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </Button>
                </DataTemplate>
            </data:DataGridTemplateColumn.CellTemplate>
        </data:DataGridTemplateColumn>
    </data:DataGrid.Columns>
</data:DataGrid>

Is it not possible to have EventTriggers in a DataGridTemplateColumn? Or do I need to declare them using alternate syntax?

Thanks

Scott Mitchell
  • 8,659
  • 3
  • 55
  • 71

1 Answers1

0

The GoToStateAction reference states that, if no target is specified, it will walk up the visual tree and apply the state change on the first element it finds that defines states. In your second case, I believe that some elements of the DataGrid visual tree (cells, rows, etc) may be taken in account before your usercontrol, thus the behavior you've seen.

Since you are using Silverlight 5, you can try to set the TargetObject property of the action to your desired control, using a binding with a relative source looking of a ancestor of the type of UserControl.

<ei:GoToStateAction StateName="MyState" TargetObject="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}"/> 
Arthur Nunes
  • 6,718
  • 7
  • 33
  • 46