MikaelKP
MikaelKP

Reputation: 133

How to get the "text" from textbox using a binding?

How do I get the content/text from a textbox using a binding? My textbox looks like this:

<TextBox x:Name="txtFields" Text="" Height="23" TextWrapping="Wrap" 
         Background="#FFCBEECD" AcceptsReturn="True" >
    <TextBox.InputBindings>
        <KeyBinding Key="Enter" Command="{Binding AddFieldCommand}"></KeyBinding>
    </TextBox.InputBindings>
</TextBox>

As seen, I would like to receive the content/text from the textbox, when the user hits the enter-button.

Thanks,

Upvotes: 0

Views: 1062

Answers (1)

Bolu
Bolu

Reputation: 8786

You need to define a property and bind it to your TextBox.Text, something like:

ViewModel:

public string TextProperty {get; set;}

XAML:

<TextBox x:Name="txtFields" Text="{Binding TextProperty, UpdateSourceTrigger=PropertyChanged}".../> 

And then you can access it (TextProperty) from the AddFieldCommand command.

Upvotes: 2

Related Questions