开发者

How to attach EventTrigger in code behind in Silverlight 4

开发者 https://www.devze.com 2023-01-13 10:37 出处:网络
My question is the following: I have a grid and I attached the SelectedIndexChanged event the following way in the xaml file:

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(selectedItemCh开发者_开发技巧angedTrigger);

            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.

0

精彩评论

暂无评论...
验证码 换一张
取 消