TriggerAction : How to bind event of DataTemplate with MVVM ?

TriggerAction : How to bind event of DataTemplate with MVVM ?

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


HK.Lee posted on Monday, May 31, 2010

Hi all,

It may not be specific to CSLA issues,but I wish to have an answer from gurus of here. Though I searched  related threads here and google, can't find right direction.

   I want to bind 'SelectionChanged' event of combobox which is embeded in gridviewDataColumn with a method  in ViewModel  . Combobox has DataTemplate as following xaml snippet and it works if I put event handler in code hehind, but not with TriggerAction.

    <telerikGrid:GridViewDataColumn Width="100" Header="자 재 명" DataMemberBinding="{Binding AccessaryName,Mode=TwoWay}">
                                            <telerikGrid:GridViewDataColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock Text="{Binding AccessaryName}"/>
                                                </DataTemplate>
                                            </telerikGrid:GridViewDataColumn.CellTemplate>
                                            <telerikGrid:GridViewDataColumn.CellEditTemplate>
                                                <DataTemplate>
                                                    <input:RadComboBox x:Name="cboAccessary" ItemsSource="{Binding Path=AccessaryList, Source={StaticResource cmodViewModelViewSource}}"
                                                              SelectedValue="{Binding AccessaryId,Mode=TwoWay}" DisplayMemberPath="AccessaryName" SelectedValuePath="Id"
                                                              SelectedItem="{Binding Path=SelectedAccessary, Mode=TwoWay, Source={StaticResource cmodViewModelViewSource}}">
                                                    </input:RadComboBox>
                                                </DataTemplate>
                                            </telerikGrid:GridViewDataColumn.CellEditTemplate>
                                        </telerikGrid:GridViewDataColumn>

.... more column definition goes

    <my:TriggerAction x:Name="accessarySelected" TargetControl="{Binding ElementName=cboAccessary}" Height="0" Width="0"
                                                                    TriggerEvent="SelectionChanged" MethodName="AddAccessaryCmodKindName"/>
          

I traced TriggerAction and found it's TargetControl dependancy property is not set on initializing because it needs Framework type but  looks combobox is still  in DataTemplate, so HookEvent method may  not be called.

Pls give me an advice how to bidn event in DataTemplate with MVVM. Thank you in advance.

HK.Lee

Chris replied on Monday, May 31, 2010

Hi HK.Lee, I also had difficulty getting to work on combobox and listbox. I ended up using the InvokeMethod from Rocky's MVVMExperimentalSL sample project. Here is an example he uses with a ListBox. Here is the code in the MVVM: public void ShowItem(object sender, ExecuteEventArgs e) { SelectedData = (Data)e.MethodParameter; } You'll notice that the event is ExecuteEventArgs, which was different than what I was expecting. This method definitely works. So, I'm using TriggerActions where I can and InvokeMethod where I can't. I don't know for sure if this will work within a Grid, because I haven't done that yet. But, it works on a standalone ListBox or ComboBox. If someone does have a solution to using TriggerAction on ComboBox, I am definitely interested to know. I would prefer to use TriggerAction throughout my app. I hope this helps, Chris

Chris replied on Monday, May 31, 2010

Wow the code all ran together when I hit post ???  Let's try this again ...

 

Hi HK.Lee,
I also had difficulty getting <TriggerAction> to work on combobox and listbox.  I ended up using the InvokeMethod from Rocky's MVVMExperimentalSL sample project.  Here is an example he uses with a ListBox.

      <ListBox ItemsSource="{Binding Model}"
               ItemTemplate="{StaticResource DataList}"
               SelectionMode="Multiple"
               x:Name="DataListBox"
               csla:InvokeMethod.Target="{Binding Source={StaticResource ListModel}}"
               csla:InvokeMethod.TriggerEvent="SelectionChanged"
               csla:InvokeMethod.MethodName="ShowItem"
               csla:InvokeMethod.MethodParameter="{Binding SelectedItem, RelativeSource={RelativeSource Self}}" />

Here is the code in the MVVM:

    public void ShowItem(object sender, ExecuteEventArgs e)
    {
      SelectedData = (Data)e.MethodParameter;
    }

You'll notice that the event is ExecuteEventArgs, which was different than what I was expecting.  This method definitely works.  So, I'm using TriggerActions where I can and InvokeMethod where I can't.  I don't know for sure if this will work within a Grid, because I haven't done that yet.  But, it works on a standalone ListBox or ComboBox.

If someone does have a solution to using TriggerAction on ComboBox, I am definitely interested to know.  I would prefer to use TriggerAction throughout my app.

I hope this helps,
Chris

 

HK.Lee replied on Monday, May 31, 2010

Hi Chris,

Thank you for your kind reply and InvokeMethod works fine even within DataTemplate of  combobox in  gridveiwDataColumn.  I also want to use TriggerAction only, which looks more elegant. Hope  Rocky or someone else may address this issue. Thanks.

HK.Lee 

HK.Lee replied on Monday, May 31, 2010

Hi Chris,

I mised one thing. In case normal stand alone combobox , which is not defined in dataTemplate, such as following xaml snippent, TriggerAction works fine.

  <input:RadComboBox Name="cboKind" ItemsSource="{Binding CmodKindList}" Margin="5" Width="300" FontSize="13"
                                                    SelectedValue="{Binding Model.CmodKindId,Mode=TwoWay}"
                                                    DisplayMemberPath="CmodKindName" SelectedValuePath="Id"
                                                    SelectedItem="{Binding Path=SelectedKind,Mode=TwoWay}"/>

   <my:TriggerAction x:Name="cmodKindSelected" TargetControl="{Binding ElementName=cboKind}" Height="0" Width="0"
                                                    TriggerEvent="SelectionChanged" MethodName="AddCmodKindName"/>

HK.Lee

RockfordLhotka replied on Tuesday, June 01, 2010

There's a term for the issue you are seeing... Hmm, if only I could think of it... Oh yeah - BUG Smile

Yes, this is most certainly a bug, and I am glad you helped identify it - thank you!

I've put the fix into svn for CSLA 4. The bug is in 3.8 too, so I'll need to fix it there as well.

HK.Lee replied on Thursday, June 03, 2010

Hi Rocky,

Thank you for your reply and just saw your video regarding CSLA 4.0 and MVVM. Hope to see as soon as possible and wish every possible cases be addressed.

Thank you in advance.

HK.Lee

rxelizondo replied on Thursday, June 03, 2010

just saw your video regarding CSLA 4.0 and MVVM

Wait... what video? where is it? I want it !! Smile

RockfordLhotka replied on Thursday, June 03, 2010

http://www.lhotka.net/weblog/CSLA4AndMVVMVideoSeries.aspx

Copyright (c) Marimer LLC