如何将参数传递给TargetedTriggerAction

时间:2014-04-14 07:36:35

标签: c# wpf xaml attachedbehaviors

我的DataGrid中有这个goto功能。这个功能我想保留我的ViewModel和代码隐藏,所以以下附件可能是完美的,但是......

用户输入一行(项目)编号,然后当用户点击GotoButton时,它会将项目带入视图。

<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>

这是TargetedTriggerAction类。

public class TargetedTriggerActionGotoButton : TargetedTriggerAction<DataGrid>
{
    protected override void Invoke(object parameter)
    {
        this.Target.SelectedGridItem = GotoLineNumber - 1;
        this.Target.SelectedGridIndex = GotoLineNumber.GetValueOrDefault() - 1;
    }
}

我想以某种方式从GotoTextbox传递文本,我可以做一些绑定吗?我怎么能做到这一点?

1 个答案:

答案 0 :(得分:1)

正如我们在评论中所说的那样 要允许传入参数,应在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;}
}

然后在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>