来自输入的WPF双空格键

时间:2013-05-27 08:19:29

标签: c# .net wpf input

当我在wpf文本框控件中输入spacebar (" ")时,它会从视图中返回double spacebar

如何配置文本框以发送原始输入文本而不是double spacebar

<DataTemplate x:Key="TextBoxDataTemplate">
    <StackPanel Orientation="Horizontal" Background="AntiqueWhite">
        <Label Name="lblTabTitle"
                                    Content="{Binding TextForLabel}"
                                    VerticalAlignment="Center"
                                    Margin="4 4 2 4"
                                    HorizontalAlignment="Left" Padding="0" />
        <TextBox Name="inputText" Width="100" Margin="5" Text ="{Binding Text}" helpers:InputTextBoxBindingsManager.UpdatePropertySourceWhenKeyPressed="TextBox.Text"/>
        <Button Name="btn"
                                    Margin="2 0 0 0"
                                    Padding="0"
                                    Height="16"
                                    Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Command="{Binding DataContext.DeleteItemCommand, ElementName=tagsList}"
                                    CommandParameter="{Binding}">
            <Image Name="img" Source="/Unive.Net;component/Images/GeneralIcons/16x16/quit.png" Width="16" Height="16" />
        </Button>
    </StackPanel>
</DataTemplate>

1 个答案:

答案 0 :(得分:1)

public static class InputTextBoxBindingsManager
{
    public static readonly DependencyProperty UpdatePropertySourceWhenKeyPressedProperty = DependencyProperty.RegisterAttached(
            "UpdatePropertySourceWhenKeyPressed", typeof(DependencyProperty), typeof(InputTextBoxBindingsManager), new PropertyMetadata(null, OnUpdatePropertySourceWhenKeyPressedPropertyChanged));

    static InputTextBoxBindingsManager()
    {

    }

    public static void SetUpdatePropertySourceWhenKeyPressed(DependencyObject dp, DependencyProperty value)
    {
        dp.SetValue(UpdatePropertySourceWhenKeyPressedProperty, value);
    }

    public static DependencyProperty GetUpdatePropertySourceWhenKeyPressed(DependencyObject dp)
    {
        return (DependencyProperty)dp.GetValue(UpdatePropertySourceWhenKeyPressedProperty);
    }

    private static void OnUpdatePropertySourceWhenKeyPressedPropertyChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
    {
        UIElement element = dp as UIElement;

        if (element == null)
        {
            return;
        }

        if (e.OldValue != null)
        {
            element.KeyUp -= HandlePreviewKeyDown;
        }

        if (e.NewValue != null)
        {
            element.KeyUp += new KeyEventHandler(HandlePreviewKeyDown);
        }
    }

    static void HandlePreviewKeyDown(object sender, KeyEventArgs e)
    {
        DoUpdateSource(e.Source);
    }

    static void DoUpdateSource(object source)
    {
        DependencyProperty property =
            GetUpdatePropertySourceWhenKeyPressed(source as DependencyObject);

        if (property == null)
        {
            return;
        }

        UIElement elt = source as UIElement;

        if (elt == null)
        {
            return;
        }

        BindingExpression binding = BindingOperations.GetBindingExpression(elt, property);

        if (binding != null)
        {
            binding.UpdateSource();
        }
    }
}