我需要在Silverlight 4应用程序中强制AutoCompleteBox内的大写输入。
在TextBox中,这可以通过替换KeyDown事件上的Text属性来完成,如:
control.Text += enteredChar;
control.Select(control.Text.Length, 0); //To maintain caret position
然而,AutoCompleteBox不提供文本选择功能,我发现没有办法将插入符号移到字符串结尾。
答案 0 :(得分:3)
您需要更改AutoCompleteBox模板并将UpperCaseBehavior添加到TextBox中:
<TextBox x:Name="Text" ...>
<i:Interaction.Behaviors>
<behaviors:UpperCaseBehavior/>
</i:Interaction.Behaviors>
</TextBox>
其中UpperCaseBehavior是:
public class UpperCaseBehavior: Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.TextChanged += AssociatedObject_TextChanged;
}
private void AssociatedObject_TextChanged(object sender, TextChangedEventArgs args)
{
var selectionStart = AssociatedObject.SelectionStart;
var selectionLength = AssociatedObject.SelectionLength;
AssociatedObject.Text = AssociatedObject.Text.ToUpper();
AssociatedObject.SelectionStart = selectionStart;
AssociatedObject.SelectionLength = selectionLength;
}
protected override void OnDetaching()
{
AssociatedObject.TextChanged -= AssociatedObject_TextChanged;
base.OnDetaching();
}
}
答案 1 :(得分:2)
您可以使用绑定并立即将键入的值转换为大写。添加自定义样式以使用textchanged事件。我测试了这个,所以如果你需要的话,如果你不能让它工作,我可以给你发一个样品。
XAML:
添加资源:
<Style x:Key="AutoCompleteBoxStyle1" TargetType="sdk:AutoCompleteBox">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFA3AEB9" Offset="0"/>
<GradientStop Color="#FF8399A9" Offset="0.375"/>
<GradientStop Color="#FF718597" Offset="0.375"/>
<GradientStop Color="#FF617584" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="#FFFFFFFF"/>
<Setter Property="Foreground" Value="#FF000000"/>
<Setter Property="MinWidth" Value="45"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="sdk:AutoCompleteBox">
<Grid Opacity="{TemplateBinding Opacity}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="PopupStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.1" To="PopupOpened"/>
<VisualTransition GeneratedDuration="0:0:0.2" To="PopupClosed"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="PopupOpened">
<Storyboard>
<DoubleAnimation To="1.0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="PopupBorder"/>
</Storyboard>
</VisualState>
<VisualState x:Name="PopupClosed">
<Storyboard>
<DoubleAnimation To="0.0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="PopupBorder"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="ValidationStates">
<VisualState x:Name="Valid"/>
<VisualState x:Name="InvalidUnfocused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="InvalidFocused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" Storyboard.TargetName="validationTooltip">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<System:Boolean>True</System:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<TextBox x:Name="Text" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" IsTabStop="True" Margin="0" Padding="{TemplateBinding Padding}" Style="{TemplateBinding TextBoxStyle}" SelectionChanged="Text_SelectionChanged"/>
<Border x:Name="ValidationErrorElement" BorderBrush="#FFDB000C" BorderThickness="1" CornerRadius="1" Visibility="Collapsed">
<ToolTipService.ToolTip>
<ToolTip x:Name="validationTooltip" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Template="{StaticResource CommonValidationToolTipTemplate}">
<ToolTip.Triggers>
<EventTrigger RoutedEvent="Canvas.Loaded">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsHitTestVisible" Storyboard.TargetName="validationTooltip">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<System:Boolean>true</System:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ToolTip.Triggers>
</ToolTip>
</ToolTipService.ToolTip>
<Grid Background="Transparent" HorizontalAlignment="Right" Height="12" Margin="1,-4,-4,0" VerticalAlignment="Top" Width="12">
<Path Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z" Fill="#FFDC000C" Margin="1,3,0,0"/>
<Path Data="M 0,0 L2,0 L 8,6 L8,8" Fill="#ffffff" Margin="1,3,0,0"/>
</Grid>
</Border>
<Popup x:Name="Popup">
<Grid Opacity="{TemplateBinding Opacity}">
<Border x:Name="PopupBorder" BorderThickness="0" Background="#11000000" HorizontalAlignment="Stretch" Opacity="0">
<Border.RenderTransform>
<TranslateTransform X="1" Y="1"/>
</Border.RenderTransform>
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" HorizontalAlignment="Stretch" Opacity="1.0" Padding="0">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFDDDDDD" Offset="0"/>
<GradientStop Color="#AADDDDDD" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Border.RenderTransform>
<TransformGroup>
<TranslateTransform X="-1" Y="-1"/>
</TransformGroup>
</Border.RenderTransform>
<ListBox x:Name="Selector" BorderThickness="0" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" ScrollViewer.HorizontalScrollBarVisibility="Auto" ItemTemplate="{TemplateBinding ItemTemplate}" ItemContainerStyle="{TemplateBinding ItemContainerStyle}" ScrollViewer.VerticalScrollBarVisibility="Auto"/>
</Border>
</Border>
</Grid>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
添加您的AutoCompleteBox并引用以前的样式:
<sdk:AutoCompleteBox Width="169" x:Name="txtSearchBox" Text="{Binding TypedText, Mode=TwoWay}" Style="{StaticResource AutoCompleteBoxStyle1}" />
代码背后:
绑定输入的文字:
private string _typedText;
public string TypedText
{
get { return _typedText; }
set
{
_typedText = value.ToUpper();
NotifyPropertyChanged("TypedText");
}
}
TextSelection事件:
private void Text_SelectionChanged(object sender, System.Windows.RoutedEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb != null && !String.IsNullOrEmpty(TypedText) && tb.SelectionStart != TypedText.Length)
{
tb.SelectionStart = TypedText.Length;
tb.SelectionLength = 0;
}
}
HTH
答案 2 :(得分:1)
它不完美,但它有效!丢失焦点事件将文本置于大写
<pre>
private void AutoCompleteTextBox_LostFocus(object sender, RoutedEventArgs e)
{
var a = (AutoCompleteTextBox)sender;
a.Text = a.Text.ToUpper();
}
</pre>
答案 3 :(得分:0)
我结束了,只使用大写字母创建字体并使用它。
答案 4 :(得分:0)
MSDN: AutoCompleteBox Styles and Templates
基本上只需在TextBox.CharacterCasing属性上使用样式设置器即可 如下:
<my:AutoCompleteBox x:Name="autoCompleteBox1">
<my:AutoCompleteBox.TextBoxStyle>
<Style TargetType="TextBox">
<Setter Property="CharacterCasing" Value="Upper" />
</Style>
</my:AutoCompleteBox.TextBoxStyle>
</my:AutoCompleteBox>
这避免了在调整文本大小时不得不解决插入位置问题的麻烦。
当然还有其他选择设置Style和Setter的位置和设置方法(我忘记了这些内容,但上面的内容似乎完成了工作)
注意:我在WPF .Net v4.0中使用它,使用AutoCompleteBox执行异步列表填充(控件不进行过滤)。