类似于Excel的行为WPF文本框 [英] Excel-like behavior WPF textbox

查看:96
本文介绍了类似于Excel的行为WPF文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个文本框,该文本绑定了一个整数(例如123456789),但是它显示了千位分隔符(例如123.456.789),但是当我选择文本框进行编辑时,字符串返回时没有分隔符,直到文本框失去了焦点,就像在Excel中一样.有什么建议吗?

I want to create a textbox which text binds an integer (e.g 123456789), but it shows the thousand separator (eg 123.456.789), but when I select the textbox for editing it the string returns without separator, until the textbox loses the focus, just like in Excel. Any advice?

推荐答案

使用触发器,如果​​未选择文本框,则该格式将格式化值

Use a Trigger which formats the value if the TextBox isn't selected

<Style TargetType="{x:Type TextBox}">
    <Setter Property="Text" Value="{Binding SomeValue, StringFormat=N2}" />
    <Style.Triggers>
        <Trigger Property="IsKeyboardFocusWithin" Value="True">
            <Setter Property="Text" Value="{Binding SomeValue}" />
        </Trigger>
    </Style.Triggers>
</Style>

如果您无法轻松地使用StringFormat进行格式化,则还可以使用Converter进行格式化

You can also use a Converter for formatting if you can't easily format with StringFormat

这篇关于类似于Excel的行为WPF文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆