none
TextChangedEventArgs.OriginalSource is null

    Question

  • Two issues:

    1.) TextChangedEventArgs.OriginalSource is always null

    2.) How do I get the last key pressed? e.Key always returns the upper case representation I need a way to discern case.


    public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                
                this.DataContext = new ViewModel();
            }
        }
    
        public class ViewModel
        {
            public ViewModel()
            {
                PerformSearch = new GalaSoft.MvvmLight.Command.RelayCommand<TextChangedEventArgs>(performSearch);
                PerformSearch2 = new GalaSoft.MvvmLight.Command.RelayCommand<KeyEventArgs>(performSearch2);
            }
    
            public GalaSoft.MvvmLight.Command.RelayCommand<TextChangedEventArgs> PerformSearch { get; set; }
            public string SearchText { get; set; }
    
            private void performSearch(TextChangedEventArgs e)
            {
                TextBox tb = ((TextBox)e.OriginalSource);       // always null
            }
    
    
            public GalaSoft.MvvmLight.Command.RelayCommand<KeyEventArgs> PerformSearch2 { get; set; }
            public string SearchText2 { get; set; }
    
            private void performSearch2(KeyEventArgs e)
            {
                TextBox tb = ((TextBox)e.OriginalSource);
                string lastKey = e.Key.ToString();          // always upper
                ModifierKeys mods = Keyboard.Modifiers;
                int code = e.PlatformKeyCode;               // no difference between "a" and "A"
    
            }
        
        }

     xmlns:gala="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"
        d:DesignHeight="300" d:DesignWidth="400">
    
        <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel Orientation="Horizontal" VerticalAlignment="Top">
                <TextBlock Text="Input: "></TextBlock>
            <TextBox Height="50" Width="150" x:Name="SearchTextBox" Text="{Binding SearchText, Mode=TwoWay}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="TextChanged">
                        <gala:EventToCommand Command="{Binding PerformSearch, Mode=OneWay}" PassEventArgsToCommand="True" ></gala:EventToCommand> 
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </TextBox>
            
            <TextBlock Text="Input: "></TextBlock>
                <TextBox Height="50" Width="150" x:Name="SearchTextBox2" Text="{Binding SearchText2, Mode=TwoWay}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="KeyUp">
                            <gala:EventToCommand Command="{Binding PerformSearch2, Mode=OneWay}" PassEventArgsToCommand="True"  ></gala:EventToCommand>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </TextBox>
    
            </StackPanel>
        </Grid>





    Wednesday, June 15, 2011 11:53 AM

All replies

  • I am also getting null for OriginalSource for TextChanged. If subscribing directly to the event (in code behind) you could use the sender property.

    I am also seeing that in the KeyEventArgs.Key is always "cap". I've noticed that Keyboard.Modifiers is always 'None' as well. Good news is that you can access OriginalSource from the KeyEventArgs.

    Thursday, June 16, 2011 12:41 AM
  • TextChangedEventArgs.OriginalSource is always null

    There is no original source for the textbox, so it is null.

    e.Key always returns the upper case representation I need a way to discern case

    e.Key returns a enum type, so it is not case sensitive, and the platformKeyCode is same because they are the same key.

    You could use a public variable to trace whether the Key.CapsLock is clicked and use ModifierKeys.Shift to trace the simple caps key.

            private bool isCaps = false;

            private void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.CapsLock)
                {
                    isCaps = !isCaps;
                    return;
                }
                if ((Keyboard.Modifiers & ModifierKeys.Shift) > 0 || isCaps)
                {
                    // Caps
                }
            }

    Sunday, June 19, 2011 10:48 PM
  • >>There is no original source for the textbox, so it is null.

    hmmm.... seems unusual.

    >> isCaps = !isCaps;

    how do I know the original state of capslock?

    Monday, June 20, 2011 10:52 AM
  • Hi,

    You can get the last char in the TextBox.Text and check the ascii number.

    The logic is like this:

    http://www.mindfiresolutions.com/How-to-check-CAPS-lock-status-in-Javascript--952.php

    Wednesday, June 22, 2011 1:27 AM
  • I solved this one by capturing the keyboard state everytime the window is Activated and then tracking the changes while the window is activated. 

    So ...

    		private void Window_Activated(object sender, EventArgs e)
    		{
    			//Get the current state of the Keyboard and setup the Status Bar accordingly.
    			Microsoft.VisualBasic.Devices.Keyboard KB = new Microsoft.VisualBasic.Devices.Keyboard();
    			if (KB.ScrollLock == true) KeyboardScrollStatus.Visibility = System.Windows.Visibility.Visible;
    			else KeyboardScrollStatus.Visibility = System.Windows.Visibility.Hidden;
    			if (KB.CapsLock == true) KeyboardCapsLockStatus.Visibility = System.Windows.Visibility.Visible;
    			else KeyboardCapsLockStatus.Visibility = System.Windows.Visibility.Hidden;
    			if (KB.NumLock == true) KeyboardNumLockStatus.Visibility = System.Windows.Visibility.Visible;
    			else KeyboardNumLockStatus.Visibility = System.Windows.Visibility.Hidden;
    		}


    And ...

    		private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
    		{
    			Microsoft.VisualBasic.Devices.Keyboard KB = new Microsoft.VisualBasic.Devices.Keyboard();
    			
    			if (e.Key == Key.Scroll)
    			{
    				if (KB.ScrollLock == true) KeyboardScrollStatus.Visibility = System.Windows.Visibility.Visible;
    				else KeyboardScrollStatus.Visibility = System.Windows.Visibility.Hidden;
    			}
    			else if (e.Key == Key.CapsLock)
    			{
    				if (KB.CapsLock == true) KeyboardCapsLockStatus.Visibility = System.Windows.Visibility.Visible;
    				else KeyboardCapsLockStatus.Visibility = System.Windows.Visibility.Hidden;
    			}
    			else if (e.Key == Key.NumLock)
    			{
    				if (KB.NumLock == true) KeyboardNumLockStatus.Visibility = System.Windows.Visibility.Visible;
    				else KeyboardNumLockStatus.Visibility = System.Windows.Visibility.Hidden;
    			}
    			else if (e.Key == Key.Insert && e.IsToggled == true)
    			{
    				KeyboardInsertStatusOn.Visibility = System.Windows.Visibility.Hidden;
    				KeyboardInsertStatusOff.Visibility = System.Windows.Visibility.Visible;
    			}
    			else if (e.Key == Key.Insert && e.IsToggled == false)
    			{
    				KeyboardInsertStatusOn.Visibility = System.Windows.Visibility.Visible;
    				KeyboardInsertStatusOff.Visibility = System.Windows.Visibility.Hidden;
    			}
    		}


    Obviously this is for tracking changes to update a status bar (a la Visual Studio) but the concept should work the same. Basically, there is no way to query the system to find the previous state of the key, so you have to track it for yourself, and you have to check to see if the state changed if the user leaves and comes back because it won't automatically tell you.

    Wednesday, June 22, 2011 11:59 AM
  • sir,

    i am able to get the eventargs using GalaSoft.MvvmLight.Command.RelayCommand<System.Windows.Input.MouseEventArgs>

    but how can i get the "sender"?? in the viewModel. using EventtoCommand

    <cmd:EventToCommand
              Command="{Binding List_PreviewMouseLeftButtonDownCommand, Mode=Default}" PassEventArgsToCommand="True"/>


    please help me out getting the "sender".


    Thanks in Advance.

    Friday, June 24, 2011 8:02 AM
  • You can add CommandParamter with ElementName with Path of Text, then it will transfer to viewmodel.

    Not need to use Arguments but parameters.

    You can ask me if you have any questions.

    Thursday, October 11, 2018 6:32 AM