2013-05-27 28 views
-1

当我在wpf文本框控件中输入spacebar (" ")时,它从视图中返回double spacebar来自输入的WPF双空格键

如何配置文本框发回原始输入文本不与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> 
+0

什么是您当前的代码来获得这个问题呢? – WiiMaxx

+0

要清楚,如果单击一次空​​格键,会得到双倍的空间! – Akrem

+0

UpdatePropertySourceWhenKeyPressed怎么样? – Akrem

回答

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(); 
     } 
    } 
}