如何将wpf AutoCompleteBox转换为所有大写输入

本文关键字:输入 转换 wpf AutoCompleteBox | 更新日期: 2023-09-27 18:13:56

我有一个AutoCompleteBox作为DataGrid列类型。像这样:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Thing, UpdateSourceTrigger=PropertyChanged}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <SLToolkit:AutoCompleteBox Text="{Binding Path=Thing,
                                              UpdateSourceTrigger=PropertyChanged}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

但是,我想将用户的输入限制为大写。在TextBoxes上,我可以这样做,但我不能让它与AutoCompleteBoxes一起工作。

<DataGridTextColumn Binding="{Binding UpdateSourceTrigger=PropertyChanged, Path=Thing}">
    <DataGridTextColumn.EditingElementStyle>
        <Style TargetType="TextBox">
            <Setter Property="CharacterCasing" Value="Upper" />
        </Style>
    </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>

我试过了:

<SLToolkit:AutoCompleteBox Text="{Binding Path=Thing,
                                          UpdateSourceTrigger=PropertyChanged}"
                           TextChanged="AutoComplete_TextChanged" />
与这个:

private void AutoComplete_TextChanged(object sender, RoutedEventArgs e)
{
     AutoCompleteBox box = sender as AutoCompleteBox;
     if (box == null) return;
     box.Text = box.Text.ToUpper();
}

除了反向写之外,它可以工作。当用户输入一个字符时,光标会回到框的开头,这样下一个单词就会在前一个单词的前面。如果我写"example",我将看到"ELPMAXE"。

任何想法?

如何将wpf AutoCompleteBox转换为所有大写输入

我解决了一个类似的问题,我只想在文本框中输入数字,所以我使用了一个行为。如果输入非数字,则删除该字符。我还使用了交互式库,它使用system . windows . interactive . DLL(只需将此DLL导入到您的项目中,如果您没有它,它是混合sdk的一部分http://www.microsoft.com/en-us/download/details.aspx?id=10801)。

下面是简化后的XAML:
    <Window x:Class="Sample.SampleWindow"
            xmlns:main="clr-namespace:MySampleApp"
            xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
            Title="Sample"
            Height="800"
            Width="1025"
            >
                         <Grid>
                            <TextBox Text="{Binding Path=Entry, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                     Width="30"
                                     MaxLength="4"
                                     HorizontalAlignment="Left">
                                <i:Interaction.Behaviors>
                                    <main:KeyPressesWithArgsBehavior 
KeyUpCommand="{Binding KeyUpFilterForUpperCaseSymbols}" />
                                </i:Interaction.Behaviors>
                            </TextBox>
                         </Grid>
    </Window>

使用以下Behavior类:

public class KeyPressesWithArgsBehavior : Behavior<UIElement>
{
    #region KeyDown Press DependencyProperty
    public ICommand KeyDownCommand
    {
        get { return (ICommand) GetValue(KeyDownCommandProperty); }
        set { SetValue(KeyDownCommandProperty, value); }
    }
    public static readonly DependencyProperty KeyDownCommandProperty =
        DependencyProperty.Register("KeyDownCommand", typeof (ICommand), typeof (KeyPressesWithArgsBehavior));
    #endregion KeyDown Press DependencyProperty
    #region KeyUp Press DependencyProperty
    public ICommand KeyUpCommand
    {
        get { return (ICommand) GetValue(KeyUpCommandProperty); }
        set { SetValue(KeyUpCommandProperty, value);}
    }
    public static readonly DependencyProperty KeyUpCommandProperty = 
        DependencyProperty.Register("KeyUpCommand", typeof(ICommand), typeof (KeyPressesWithArgsBehavior));
    #endregion KeyUp Press DependencyProperty
    protected override void OnAttached()
    {
        AssociatedObject.KeyDown += new KeyEventHandler(AssociatedUIElementKeyDown);
        AssociatedObject.KeyUp += new KeyEventHandler(AssociatedUIElementKeyUp);
        base.OnAttached();
    }
    protected override void OnDetaching()
    {
        AssociatedObject.KeyDown -= new KeyEventHandler(AssociatedUIElementKeyDown);
        AssociatedObject.KeyUp -= new KeyEventHandler(AssociatedUIElementKeyUp);
        base.OnDetaching();
    }
    private void AssociatedUIElementKeyDown(object sender, KeyEventArgs e)
    {
        if (KeyDownCommand != null)
        {
            ObjectAndArgs oa = new ObjectAndArgs {Args = e, Object = AssociatedObject};
            KeyDownCommand.Execute(oa);
        }
    }
    private void AssociatedUIElementKeyUp(object sender, KeyEventArgs e)
    {
        if (KeyUpCommand != null)
        {
            KeyUpCommand.Execute(AssociatedObject);
        }
    }
}

然后在你的视图模型中你可以实现这个命令。SampleWindowViewModel.cs:

    public ICommand KeyUpFilterForUpperCaseSymbolsCommand
    {
        get
        {
            if (_keyUpFilterForUpperCaseSymbolsCommand== null)
            {
                _keyUpFilterForUpperCaseSymbolsCommand= new RelayCommand(KeyUpFilterForUpperCaseSymbols);
            }
            return _keyUpFilterForUpperCaseSymbolsCommand;
        }
    }

    private void KeyUpFilterForUpperCaseSymbols(object sender)
    {
        TextBox tb = sender as TextBox;
        if (tb is TextBox)
        {
            // check for a lowercase character here
            // then modify tb.Text, to exclude that character.
            // Example: tb.Text = oldText.Substring(0, x);
        }
    }