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.