views:

18

answers:

0

Hi,

My question is the following:

I have a grid and I attached the SelectedIndexChanged event the following way in the xaml file:

"<cc:DetailViewGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="dgAcitivityList" VerticalAlignment="Stretch" ItemsSource="{Binding EntityList}" SelectionMode="Single" IsReadOnly="False">
            <interactivity:Interaction.Triggers>
                <interactivity:EventTrigger EventName="SelectionChanged">
                    <interactivity:InvokeCommandAction Command="{Binding SelectedItemChangeCommand}" CommandParameter="{Binding SelectedItem, ElementName=dgAcitivityList}"/>
                </interactivity:EventTrigger>
            </interactivity:Interaction.Triggers>"

But I want to attach this event in code behind. I ctreated an own grid that is inherited from windows grid, and I put this code to own control.

   public override void OnApplyTemplate()
        {
            //base.OnApplyTemplate();

            System.Windows.Interactivity.EventTrigger selectedItemChangedTrigger = new System.Windows.Interactivity.EventTrigger("SelectionChanged");

            System.Windows.Interactivity.InvokeCommandAction action = new System.Windows.Interactivity.InvokeCommandAction();

            action.CommandName = "{Binding SelectedItemChangeCommand}";

            action.CommandParameter = string.Format("{{Binding SelectedItem, ElementName={0}}}", this.Name);

            selectedItemChangedTrigger.Actions.Add(action);

            System.Windows.Interactivity.Interaction.GetTriggers(this).Add(selectedItemChangedTrigger);

            base.OnApplyTemplate();
        }

Is this solution proper? It's not working but I'm not sure that I should put this code in the OnApplyTemplate() method.