代码之家  ›  专栏  ›  技术社区  ›  Greg D

创建用下划线替换空格的WPF文本框的最佳方法是什么?

  •  3
  • Greg D  · 技术社区  · 14 年前

    我正在创造一个 FilteredTextBox 在wpf中,将包含的 TextBox 控制。这个 文本框过滤器 必须只允许范围内的字符 [a-zA-Z0-9_] 要进入,我已经有相当多的工作。我已经迷上了 OnPreviewTextInput 要处理键入的字符, OnPreviewDrop 为了过滤拖放的字符,我添加了一个PreviewExecutedHandler,它在每次对控件执行命令以处理粘贴时都会运行。

    到目前为止,一切都很好。

    棘手的是,控件在键入空格时还应使用下划线替换空格。

    我已经想出了一个解决方案,但它感觉被很好地拼凑在一起,我不知道它遗漏了什么。我觉得必须有一种更好的技术,我只是不知道。我所做的一切:

    internal class FilteredTextBox : TextBox
    {
        public FilteredTextBox()
        {
            CommandManager.AddPreviewExecutedHandler(this, this.HandlePreviewExecuteHandler);
        }
    
        private void HandlePreviewExecuteHandler(object sender, ExecutedRoutedEventArgs e)
        {
            var uiCmd = e.Command as RoutedUICommand;
            if (uiCmd != null && (uiCmd.Text == "Space" || uiCmd.Text == "ShiftSpace"))
            {
                // We're manually handling spaces, so we need to make appropriate checks.
                if (this.Text.Length == this.MaxLength) return;
    
                if (this.SelectionLength == 0)
                {
                    // If the user is just typing a space normally
                    // We need to cache CaretIndex b/c it's reset to 0 when we set Text.
                    var tmpIndex = this.CaretIndex;
                    this.Text = this.Text.Insert(tmpIndex, "_");
                    this.CaretIndex = tmpIndex + 1;
                }
                else
                {
                    // Otherwise, replace the selected text with the underscore and fixup the caret.
                    this.SelectedText = "_";
                    this.CaretIndex += this.SelectedText.Length;
                    this.SelectionLength = 0;
                }
    
                e.Handled = true; // If someone hits the spacebar, say we handled it.
                return;
            }
        }
    }
    

    有更聪明的方法吗?

    3 回复  |  直到 13 年前
        1
  •  3
  •   Robert Rossney    14 年前

    OnPreviewKeyDown

        2
  •  4
  •   bufferz    14 年前

    TextBox ValueConverter

    public class SpaceConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return System.Convert.ToString(value); 
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string text = System.Convert.ToString(value);
    
                //the meat and potatoes is this line
                text = text.Replace(" ", "_");    
    
                return text;
            }
        }
    

    <TextBox Text="{Binding Path=UserString, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource SpaceConverter}}" />
    

    UserString DataContext

    SpaceConverter UserControl

    <UserControl.Resources>
       <local:SpaceConverter x:Key="SpaceConverter" />
    </UserControl.Resources>
    

    local

        3
  •  1
  •   Viv    14 年前