C# 来自输入的WPF双空格键
当我在wpf文本框控件中键入C# 来自输入的WPF双空格键,c#,.net,wpf,input,C#,.net,Wpf,Input,当我在wpf文本框控件中键入空格键(“”)时,它会从视图中返回双空格键 如何将textbox配置为不使用双空格键发回原始输入文本 <DataTemplate x:Key="TextBoxDataTemplate"> <StackPanel Orientation="Horizontal" Background="AntiqueWhite"> <Label Name="lblTabTitle"
空格键(“”)时,它会从视图中返回双空格键
如何将textbox配置为不使用双空格键
发回原始输入文本
<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>
您当前的代码是什么?请澄清,如果单击空格键一次,您将获得两倍的空格!?UpdatePropertySourceWhenKeyPressed呢?是的,当我单击它一次时,视图中有两倍的空间。您可以显示UpdatePropertySourceWhenKeyPressed
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();
}
}
}