I am having an issue , the textbov does allow space when pressing space bar

Sgcino Ngema 0 Reputation points
2024-07-04T11:30:05.2+00:00
								<TextBox

									x:Name="txtLocationName"

									Grid.Column="3"

									Grid.Row="5"

									Height="22"

									VerticalAlignment="Center"

									TabIndex="17"

									IsEnabled="{Binding boolEnableLocationName}"

									Text="{Binding Path=strSearchLocationNameCaller, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True}"

									PreviewKeyDown="txtLocationName_PreviewKeyDown"></TextBox>
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,769 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hongrui Yu-MSFT 1,765 Reputation points Microsoft Vendor
    2024-07-05T03:22:58.98+00:00

    Hi,@Sgcino Ngema. Welcome to Microsoft Q&A. 

    Note: The space bar is also a normal input in a TextBox.

     

    If you want to enter data without preserving space bar, you could try using Trim().

     

    Here is a reference example:

    
    <Grid>
    
        <TextBox x:Name="txtLocationName" Grid.Column="3" Grid.Row="5" Height="22" VerticalAlignment="Center" TabIndex="17" IsEnabled="{Binding BoolEnableLocationName}"
            Text="{Binding Path=StrSearchLocationNameCaller, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True}"
            PreviewKeyDown="txtLocationName_PreviewKeyDown">
        </TextBox>
    
    </Grid>
    
    

     

    
    public partial class CaptureUC : UserControl,INotifyPropertyChanged
    
    {
    
       public event PropertyChangedEventHandler PropertyChanged;
    
     
    
        private string strSearchLocationNameCaller;
    
     
    
        public string StrSearchLocationNameCaller { get { return strSearchLocationNameCaller; } set { strSearchLocationNameCaller = value.Trim(); OnPropertyChanged("StrSearchLocationNameCaller"); } }
    
     
    
        private bool boolEnableLocationName;
    
     
    
        public bool BoolEnableLocationName { get { return boolEnableLocationName; } set { boolEnableLocationName = value; OnPropertyChanged("BoolEnableLocationName"); } }
    
     
    
        public CaptureUC()
    
        {
    
            InitializeComponent();
    
            BoolEnableLocationName = true;
    
            this.DataContext = this;
    
        }
    
     
    
        private void txtLocationName_PreviewKeyDown(object sender, KeyEventArgs e)
    
        {
    
               
    
        }
    
     
    
        private void OnPropertyChanged(string name)
    
        {
    
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    
        }
    
    }
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.