0

This is my view (TypeAheadTextBox.xaml)


    <TextBox x:Name="textBox" Width="300" Text="{Binding SomeText, UpdateSourceTrigger=PropertyChanged}" TextChanged="textBox_TextChanged_1" SelectionChanged="textBox_SelectionChanged">
      <TextBox.InputBindings>                
        <KeyBinding Command="{Binding LeftCtrlKeyPressed, Mode=TwoWay}" Key="Space"  Modifiers="Control" />
        <KeyBinding Key="Down" Command="{Binding TextBoxDownArrow, Mode=TwoWay}" />
      </TextBox.InputBindings>
    </TextBox>

This is my view cs file(TypeAheadTextBox.xmal.cs)


public partial class TypeAheadControl
{
    public TypeAheadControl()
    {
        InitializeComponent();
    }


    private void textBox_TextChanged_1(object sender, TextChangedEventArgs e)
    {
        SetCommandParameter(sender);
    }

    private void textBox_SelectionChanged(object sender, RoutedEventArgs e)
    {
        SetCommandParameter(sender);
    }

    private void SetCommandParameter(object sender)
    {
        TextBox textBox = sender as TextBox;
        if (textBox != null && textBox.Text.Length > 0)
        {
            KeyBinding kb = textBox.InputBindings[0] as KeyBinding;
            if (kb != null)
            {
                string[] words = textBox.Text.Split(new char[] { ' ' });
                if (textBox.CaretIndex == textBox.Text.Length)
                {
                    //return last word
                    kb.CommandParameter = words[words.Length - 1];
                    Console.WriteLine(words[words.Length - 1]);
                }
                else
                {
                    int charCount = 0;
                    foreach (string word in words)
                    {
                        charCount += word.Length;
                        if (charCount >= textBox.CaretIndex)
                        {
                            kb.CommandParameter = word;
                            Console.WriteLine(word);
                            break;
                        }
                    }
                }
            }
        }
    }

}

And this is my ViewModel Class (part of it)


  private DelegateCommand _leftCtrlKeyPressed;

     public ICommand LeftCtrlKeyPressed
    {
        get
        {
            if (_leftCtrlKeyPressed == null)
            {
                _leftCtrlKeyPressed = new DelegateCommand(CtrlKeyDetected);
            }
            return _leftCtrlKeyPressed;  
        }
        set { }
    }

     public void CtrlKeyDetected()
     {

         Console.WriteLine("Test=====>>" + CurrentWord);
     }

My Problem :

Now I want to access the Textbox keybinding command parameter value in my viewModel deligateCommand Action

CtrlKeyDetected().

Can somebody please tell me how can I achieve that.

What the program actually do ?

Suppose you have write down some text in the text box and placed your cursor over a word and my target is to get the current word under the current position of the cursor. As soon as the user press ctrl+space key i want to get the value in the command binding method.

3
  • do you need the last word in command parameter? Commented Sep 25, 2014 at 6:44
  • no. The word under the cursor. Commented Sep 25, 2014 at 6:50
  • @Default: You are right. Generic version of delegate command solve my issue. Commented Sep 26, 2014 at 9:07

1 Answer 1

1

If you're using for instance PRISMs DelegateCommand there should be a generic version as well. The generic version accepts command parameters, so the following should work:

private DelegateCommand<string> _leftCtrlKeyPressed;

public ICommand LeftCtrlKeyPressed
{
    get
    {
        if (_leftCtrlKeyPressed == null)
        {
            _leftCtrlKeyPressed = new DelegateCommand<string>(CtrlKeyDetected);
        }
        return _leftCtrlKeyPressed;  
    }
    set { }
}

 public void CtrlKeyDetected(string parameter)
 {

     Console.WriteLine("Test=====>>" + parameter);
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.