How to pass parameter to TargetedTriggerAction
-
23-12-2019 - |
Question
I've got this goto functionality in my DataGrid. This functionality I would like to keep out of my ViewModel and out of code-behind, so the following attachment could be perfect, however...
The user enters a line(item) number then when the user clicks the GotoButton it brings the item into view.
<Grid>
<TextBox x:Name="GotoTextbox" Text="{Binding GotoLineNumber, UpdateSourceTrigger=PropertyChanged}" />
<Button Name="GotoButton" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<helpers:TargetedTriggerActionGotoButton TargetObject="{Binding ElementName=GenericDataGrid}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
Here is the TargetedTriggerAction class.
public class TargetedTriggerActionGotoButton : TargetedTriggerAction<DataGrid>
{
protected override void Invoke(object parameter)
{
this.Target.SelectedGridItem = GotoLineNumber - 1;
this.Target.SelectedGridIndex = GotoLineNumber.GetValueOrDefault() - 1;
}
}
I would like to somehow pass the text in from GotoTextbox, is there some binding I can do?. How could I achieve this?
Solution
As we spoke in comments
to allow parameters to be passed in, one should implement additional property in your TargetedTriggerAction
public class TargetedTriggerActionGotoButton : TargetedTriggerAction<DataGrid>
{
protected override void Invoke()
{
this.Target.SelectedGridItem = GotoLineNumber - 1;
this.Target.SelectedGridIndex = GotoLineNumber.GetValueOrDefault() - 1;
}
//property used as parameter
public object Parameter {get;set;}
}
and then in your xaml
<Grid>
<TextBox x:Name="GotoTextbox" Text="{Binding GotoLineNumber, UpdateSourceTrigger=PropertyChanged}" />
<Button Name="GotoButton" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<helpers:TargetedTriggerActionGotoButton TargetObject="{Binding ElementName=GenericDataGrid}" Parameter="{Binding ElementName="/*desiredName*/",Path="/*neededValue*/"}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow