locked
Interactivity to a combobox: cannot add EventTrigger (WPF, VS2008) RRS feed

  • Question

  • Hi All,

    I am using WPF and VS2008.

    I am trying to add interactivity to a combobox, so that I can add a command to it. I basically want a command to fire when the selected object is changed:

    <Window x:Class="InteractivityTest.Window1"
    
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    
     Title="Window1" Height="300" Width="300"
    
     xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"   
    
     xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"  
    
     xmlns:il="clr-namespace:Microsoft.Expression.Interactivity.Layout;assembly=Microsoft.Expression.Interactions">
    
     <Grid>
    
      <ComboBox Name="ProfileOptionsComboBox">
    
       <i:Interaction.Triggers>    
    
        <i:EventTrigger EventName="SelectionChanged">
    
         <i:InvokeCommandAction
    
           CommandName="ApplicationCommand.New"
    
           CommandParameter="{Binding SelectedItem, ElementName=ProfileOptionsComboBox, Mode=OneWay}" />
    
        </i:EventTrigger>
    
       </i:Interaction.Triggers>
    
      </ComboBox>
    
     </Grid>
    
    </Window>

    However when I run this I get the following error:

    Cannot add content of type 'System.Windows.Interactivity.EventTrigger' to an object of type 'System.Windows.Interactivity.TriggerCollection'.  Error at object 'System.Windows.Interactivity.EventTrigger' in markup file 'InteractivityTest;component/window1.xaml' Line 16 Position 19.

    I have tried searching for examples of TriggerCollection but can find no XAML examples.

    Furthermore, my System.Windows.Interactivity does not provide any intellisense suggestion for TriggerCollection in XAML (but it is there in the code intellisense). - I don't actually think this is relevent to the problem though! :)

    Any ideas?

    Thanks,

    Leon

    • Edited by Iobetes Friday, May 21, 2010 3:08 PM not being able to write a coherent post
    Friday, May 21, 2010 1:13 PM

All replies

  • "Furthermore, my System.Windows.Interactivity"

     

    I suspect the end of that sentence has something to do with the problem, could you fill that in? :)

    Friday, May 21, 2010 1:46 PM
    Moderator
  • This may be little late for the person who made the post.. but It may be helpful for someone who is facing such kinda problem in v3.5. At least for me it worked.

    create a class like:

     public class CommonActions : TriggerAction<FrameworkElement>
        {
            public static readonly DependencyProperty CommandProperty =
    DependencyProperty.Register("Command", typeof(string), typeof(FrameworkElement));

            public string Command
            {
                get { return base.GetValue(CommandProperty) as string; }
                set { base.SetValue(CommandProperty, value); }
            }

            public static readonly DependencyProperty CommandParameterProperty =
    DependencyProperty.Register("CommandParameter", typeof(string), typeof(FrameworkElement));

            public string CommandParameter
            {
                get { return base.GetValue(CommandParameterProperty) as string; }
                set { base.SetValue(CommandParameterProperty, value); }
            }
            protected override void Invoke(object param)
            {
                if (Command != null)
                {
                    object ctx =  AssociatedObject.DataContext;
                    if (ctx != null)
                    {
                        var cmd = ctx.GetType().GetProperty(Command).GetValue(ctx, null) as ICommand;
                        object o = AssociatedObject.GetType().GetProperty(CommandParameter).GetValue(AssociatedObject,null);
                        if (cmd != null && cmd.CanExecute(o))
                            cmd.Execute(o);
                    }
                }
            }
        }


    Add these namespaces in XAML:

      xmlns:ei="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
                xmlns:i="clr-namespace:MDCNet.Interactions"

    and use in the control like:

     <ei:Interaction.Triggers>
                                    <ei:EventTrigger EventName="SelectionChanged">
                                        <i:CommonActions Command="SetEditNameCommand" CommandParameter="SelectedIndex"/>
                                    </ei:EventTrigger>
                                </ei:Interaction.Triggers>


    • Edited by VibhorJain Monday, June 3, 2013 6:13 AM
    Monday, June 3, 2013 6:10 AM