alerya
alerya

Reputation: 3175

KeyTrigger passing parameters

I have a textbox and want to pass text entered in command. I do follwong:

XAML:

<TextBox Margin="0" Grid.Row="2" TextWrapping="Wrap" >
            <i:Interaction.Triggers>
                <iex:KeyTrigger Key="Enter">
                    <i:InvokeCommandAction Command="{Binding TextBoxMessageCommand}"  CommandParameter="{Binding Path=Text, RelativeSource={RelativeSource Self}}"/>
                </iex:KeyTrigger>
            </i:Interaction.Triggers>
        </TextBox>

Code in Command:

public override void Execute(object parameter)
    {
       string msg = parameter;

It's OK. Method Execute fires properly and nice. But parameter == null. What is wrong here ?

Upvotes: 0

Views: 262

Answers (1)

Benlitz
Benlitz

Reputation: 2002

You should try to give a name to your TextBox and reference it via this name in the command. Self won't work from within a command.

<TextBox x:Name="textBox" Margin="0" Grid.Row="2" TextWrapping="Wrap" >
        <i:Interaction.Triggers>
            <iex:KeyTrigger Key="Enter">
                <i:InvokeCommandAction Command="{Binding TextBoxMessageCommand}"  CommandParameter="{Binding Path=Text, ElementName=textbox}"/>
            </iex:KeyTrigger>
        </i:Interaction.Triggers>
</TextBox>

Upvotes: 1

Related Questions