This project is read-only.

Binding TriggerAction to button within a ListView Column

Sep 23, 2011 at 9:25 PM

Can someone explain how to Bind a TriggerAction to a Click Event of a Button which exists within a Column of a ListView? 

I can get the Button to trigger a click event in the code behind but would rather capture the event with a TriggerAction and have it trigger a method in a ViewModel.


<ListView x:Name="ListViewName" Grid.Row="3" Margin="5 0 5 20" Width="120" HorizontalAlignment="Right" VerticalAlignment="Stretch" 
									ItemsSource="{Binding Path=Model.ColorMapItems, Mode=TwoWay}" 
					  Background="{StaticResource MutedBlackBackgroundBrush}" BorderBrush="Black" BorderThickness="1.0">
					<GridView AllowsColumnReorder="False">
						<GridViewColumn Header="Heading"   >
									<TextBlock Margin="1" Text="{Binding Path=Something, Mode=TwoWay}" Foreground="LightGray"
										Background="{StaticResource MutedBlackBackgroundBrush}" />
						<GridViewColumn x:Name="ColumnName" Header="Color">
									<Button x:Name="ButtonNameWithinAColumn" Margin="1" ToolTip="{Binding Path=SomethingElse, Mode=TwoWay}" 
											Foreground="LightGray" Height="18" Width="18"
											Background="{Binding Path=ASolidColorBrush}" />

<bxf:TriggerAction Height="0" Width="0" x:Name="BrushChangeTrigger" TargetControl="{Binding ElementName=ListViewName, Path=ColumnName.ButtonNameWithinAColumn}" MethodName="OnButtonClickInViewModelMethod"/>

Sep 23, 2011 at 10:39 PM

Just bind it to the button TargetControl="{Binding ElementName  = ButtonName}"




Sep 27, 2011 at 4:26 PM

I have tried that.  It does not work.  I am not sure how to get the TargetControl property of the TriggerAction set correctly to initiate the trigger.

Sep 27, 2011 at 6:39 PM

Put the TriggerAction control in the same scope as the Button. In your example, the TriggerAction isn't in the same part of the visual tree as the Button. They aren't in the same scope, nor is the Button an ancestor of the TriggerAction, so I don't think there's a binding expression that will find the Button.

Sep 28, 2011 at 3:47 AM

Thanks for the insight.  This worked.  I never considered placing the TriggerAction within the content's of the datatemplate.  This is opening up yet another part of XAML I did not expect.