【问题标题】:How to convert wpf AutoCompleteBox to all uppercase input如何将 wpf AutoCompleteBox 转换为所有大写输入
【发布时间】:2015-12-20 00:02:06
【问题描述】:

我有一个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();
}

这种工作除了它向后写。当用户输入一个字符时,光标会回到框的开头,因此下一个单词在前一个单词的前面。如果我写“示例”,我会看到“ELPMAXE”。

有什么想法吗?

【问题讨论】:

    标签: c# wpf xaml wpf-controls wpftoolkit


    【解决方案1】:

    我解决了一个类似的问题,我只想在文本框中输入数字,所以我使用了一个行为。如果输入非数字,则删除该字符。我还使用了使用 System.Windows.Interactivity.dll 的交互库(只需将此 DLL 导入您的项目,如果您没有它,它是 blend 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);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 2022-01-24
      • 2014-02-19
      相关资源
      最近更新 更多