如何将wpf AutoCompleteBox转换为全部大写输入

时间:2015-09-22 20:43:35

标签: c# wpf xaml wpf-controls wpftoolkit

我有一个# Allow the use of aggregates mondrian.rolap.aggregates.Use=false mondrian.rolap.aggregates.Read=false mondrian.native.topcount.enable=true mondrian.native.filter.enable=true mondrian.rolap.aggregates.optimizePredicates=true # mondrian.properties # mondrian.result.limit=50000 # For XML/A JSPs mondrian.test.connectString=Provider=mondrianJdbc=jdbc:odbc:MondrianFoodMart;JdbcDrivers=sun.jdbc.odbc.JdbcOdbcDriver;Catalog=/WEB-INF/queries/FoodMart.xml; mondrian.result.limit=5000000 # Caching Parameters mondrian.expCache.enable=true mondrian.rolap.EnableRolapCubeMemberCache=true mondrian.rolap.star.disableCaching=false # mondrian.native.ExpandNonNative=true mondrian.rolap.EnableRolapCubeMemberCache=true # Default is 1000 mondrian.xmla.drillthroughMaxRows=5000 # Default is true mondrian.util.memoryMonitor.enable=false # Miscelleneous # These values should be coprime to each other mondrian.result.highCardChunkSize=113 # mondrian.query.limit=97 mondrian.rolap.maxQueryThreads=100 作为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" />

这种作品除了向后写。当用户输入字符时,光标返回到框的开头,因此下一个字位于前一个字的前面。如果我写'例子',我会看到“ELPMAXE”。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我解决了类似的问题,我只想在文本框中输入数字,所以我使用了一种行为。如果输入非数字,则删除该字符。我还使用了使用System.Windows.Interactivity.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);
        }
    }
}

然后在View Model中,您可以实现该命令。 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);
        }
    }