开发者

Proper case text as it is being entered in WPF

How do I proper case text as the user enters it in a WPF form. I'm using 开发者_StackOverflow社区the following code to do the proper casing which works fine, but I can't figure out how to do it on user entry.

Microsoft.VisualBasic.Strings.StrConv(txt.Text,VbStrConv.ProperCase,0);


I'm a bit late into this but the following dll capitalises as you type in any WPF control, including after full stops (that's periods for those on the other side of the water!), commas, etc so it acts a bit more like Word...

http://www.mardymonkey.co.uk/blog/index.php/2010/01/auto-capitalise-a-text-control-in-wpf/


You'll want to use code like you have, or Jobi posted, in the Key_Down (or Up) event of the text control.


        TextInfo txtinfo = Thread.CurrentThread.CurrentCulture.TextInfo;
        string titleCaseString = txtinfo.ToTitleCase(theString);

To work this in the DataBinding, you can write a WPF Binding Converter and add this logic there.

        <TextBox>
         <TextBox.Text>
            <Binding Path="ViewModelProperty" Converter="{StaticResource TitleCaseConverter}" UpdateSourceTrigger="PropertyChanged" />
         </TextBox.Text>
       </TextBox>


In our application we have a custom TextBox where we have overridden the PreviewTextInput event to perform validation there (we are validating the input against a regex and preventing the character from being added if it is invalid) - maybe you could do something similar?

I have had problems with custom TextBoxed before though, the caret jumping back to the start of the string was something I couldn't quite grasp either, can't remember how we got around it though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜