TriggerAction customization

TriggerAction customization

Old forum URL: forums.lhotka.net/forums/t/12044.aspx


TygreWolf posted on Thursday, June 27, 2013

Two questions relating to the TriggerAction control and the XAML around it.  First, I've read Rocky's blog post here about the control.  My questions are:

1. Does the TriggerEvent attribute absolutely have to be in the format

public void MyAction(object sender, ExecuteEventArgs e)

or can it be:

public void MyAction(object sender, MyOwnEventArgs e) where MyOwnEventArgs inherits directly from System.EventArgs?

2. After the TriggerEvent is defined, how can I use MethodParameter to define a particular parameter from the EventArgs to pass into a specific function (MethodName) that doesn't have the same signature?

Example: MyOwnEventArgs contains a UserInfo object.  The Method which the TriggerAction will call only takes a UserInfo object.  What do I put as a MethodParameter value to pull the UserInfo object out of my event args and pass it to the LoadUser method?

<
v:UserList x:Name="UserList"/>
<csla:TriggerAction x:Name="UserSelectedTrigger" 
TargetControl
="{Binding ElementName=UserList}" 
TriggerEvent
="OnUserSelected" 
MethodName
="LoadUser" 
MethodParameter
="???"/>


-Jason

TygreWolf replied on Thursday, June 27, 2013

Alright, I've simplified the design of my view a little bit.  I'm attempting to call a method "LoadUser" on my ViewModel when an item is selected on a datagrid.  I'm using a CSLA TriggerAction to do via the following code:

<DataGrid x:Name="DgUsers" ItemsSource="{Binding Source={StaticResource UserListModelViewSource}}"
              AlternatingRowBackground="AliceBlue" SelectionMode="Single" CanUserResizeRows="False" DockPanel.Dock="Top"
              HorizontalAlignment="Stretch" ColumnWidth="*" CanUserAddRows="False" AutoGeneratingColumn="DgUsers_AutoGeneratingColumn">
</DataGrid>
<csla:TriggerAction x:Name="SelectedUserTrigger" TargetControl="{Binding ElementName=DgUsers, Path=SelectedItem}" MethodName="LoadUser" DockPanel.Dock="Bottom"/>

At this point, I get first chance exceptions (2 similar, but not exact errors) occurring when I select a user in the grid:

System.Windows.Data Error: 23 : Cannot convert 'Jane Smith' from type 'UserInfo' to type 'System.Windows.FrameworkElement' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: TypeConverter cannot convert from STIPEMSimulator.Library.BusinessObjects.Users.UserInfo.
   at System.ComponentModel.TypeConverter.GetConvertFromException(Object value)
   at System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)'
A first chance exception of type 'System.NotSupportedException' occurred in PresentationFramework.dll
System.Windows.Data Error: 6 : 'ObjectSourceConverter' converter failed to convert value 'Jane Smith' (type 'UserInfo'); fallback value will be used, if available. BindingExpression:Path=SelectedItem; DataItem='DataGrid' (Name='DgUsers'); target element is 'TriggerAction' (Name='SelectedUserTrigger'); target property is 'TargetControl' (type 'FrameworkElement') NotSupportedException:'System.NotSupportedException: TypeConverter cannot convert from STIPEMSimulator.Library.BusinessObjects.Users.UserInfo.
   at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)
   at MS.Internal.Data.ObjectSourceConverter.Convert(Object o, Type type, Object parameter, CultureInfo culture)
   at System.Windows.Data.BindingExpression.ConvertHelper(IValueConverter converter, Object value, Type targetType, Object parameter, CultureInfo culture)'

Any thoughts here?  The dgUsers data grid is bound to a CSLA ViewModel<UserList>, where UserList is a ReadOnlyList of UserInfo objects.

TygreWolf replied on Thursday, June 27, 2013

Nevermind, found my answer.

The correct usage is:

<csla:TriggerAction x:Name="SelectedUserTrigger" TargetControl="{Binding ElementName=DgUsers}" TriggerEvent="SelectionChanged" MethodName="LoadUser" />

Copyright (c) Marimer LLC