Wednesday, August 29, 2012

Event trigger is not working inside ItemControl

Event trigger is not working inside ItemControl

I have a Item control which fills by a list, and list is collection of two parameters 'Time' and 'Description'. For it, i am using a HyperLinkButton for time and a Label for description.

What i want is that, i want to create click event using EventTrigger of hyperLink button in Main viewModel. My code is:

<ItemsControl      x:Name="transcriptionTextControl"      ItemsSource="{Binding MyCollectionOfTranscription, Mode=TwoWay}">     <ItemsControl.ItemTemplate>         <DataTemplate>             <StackPanel>                 <HyperlinkButton Content="{Binding Time}">                     <ToolTipService.ToolTip>                         <ToolTip Content="Time"/>                     </ToolTipService.ToolTip>                     <i:Interaction.Triggers>                         <i:EventTrigger EventName="Click">                             <i:InvokeCommandAction                                  Command="{Binding HyperLinkButtonCommand}"                                  CommandParameter="{Binding                                      ElementName=transcriptionTextControl }" />                         </i:EventTrigger>                     </i:Interaction.Triggers>                 </HyperlinkButton>                 <sdk:Label Content="{Binding Description}"/>             </StackPanel>         </DataTemplate>     </ItemsControl.ItemTemplate> </ItemsControl> 

When i build project, it doesn't give error but ICommand for hyperLink, shows warning as 'Cannot resolve symbol HyperLinkButtonCommand', while this event trigger is working fine outside this .

Not getting, what is actual problem behind it, plz give your valuable suggestion...

Answers & Comments...

Answer: 1

First off,

<i:InvokeCommandAction      Command="{Binding HyperLinkButtonCommand}"      CommandParameter="{Binding          ElementName=transcriptionTextControl }" /> 

The Binding is trying to locate a property called HyperLinkButtonCommand on the instance of the type that is contained within MyCollectionOfTranscription (you don't need to bind to this two-way).

(Side note, sending an ItemsControl into your Command is not MVVM.)

The ItemsControl iterates through each element in this collection, creates a copy of the template defined in ItemsControl.ItemTemplate, and sets the BindingContext equal to this element (I assume its a Transcript). You can tell this from the warnings you get from the binding failing to find your HyperLinkButtonCommand if you crank up databinding debug settings.

enter image description here

Assuming

  1. HyperLinkButtonCommand is a command defined in your ViewModel, and
  2. The root of this xaml is a Window (could be a UserControl, but am assuming here)
  3. Your ViewModel is the DataContext of the Window

you can change the binding to the following and it should work (or you should get a clue from it)

<i:InvokeCommandAction      Command="{Binding HyperLinkButtonCommand,                RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"      CommandParameter="{Binding          ElementName=transcriptionTextControl }" /> 

I prefer just to give my root an x:Name of "root" and use "ElementName=root" in cases like this.

by : Willhttp://stackoverflow.com/users/1228




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog