0

当用户在文本框中(在 silverlight 2.0 中)时,我想要做什么:

  • 当用户按下数字键盘上的小数点 (.)时,我希望将其替换为正确的小数分隔符(在很多国家/地区都是逗号 (,))

我可以通过签入 keydown 事件来跟踪用户输入了小数点

void Cell_KeyDown(object sender, KeyEventArgs e)
{
     if (e.Key == Key.Decimal)

但是如何在 Silverlight 中用另一个键替换该键。e.Key是只读的。有没有办法向控件“发送另一个密钥”?或者有什么其他建议?

4

8 回答 8

2

答案是在 MSDN 的网站上找到的: Replace-numpad-decimalpoint

Imports System.Threading
Imports System.Windows.Forms

Namespace My


    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.

    Partial Friend Class MyApplication
        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            If Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator <> "." Then
                System.Windows.Forms.Application.AddMessageFilter(New CommaMessageFilter)

            End If
        End Sub

    End Class

    Friend Class CommaMessageFilter
        Implements IMessageFilter
        Private Const WM_KEYDOWN = &H100

        Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements IMessageFilter.PreFilterMessage

            If m.Msg = WM_KEYDOWN Then
                Dim toets As Keys = CType(CType(m.WParam.ToInt32 And Keys.KeyCode, Integer), Keys)
                If toets = Keys.Decimal Then
                    SendKeys.Send(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator)
                    Return True
                End If
            End If
            Return False
        End Function
    End Class

End Namespace
于 2012-04-23T11:40:17.917 回答
1
public class NumericUpDown : System.Windows.Controls.NumericUpDown
{
    [DebuggerStepThrough]
    protected override double ParseValue(string text)
    {
        text = text.Replace(".", Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);
        return base.ParseValue(text);
    }
}

干杯

于 2011-08-31T13:44:57.503 回答
1

我有一个我在我的winforms应用程序中使用的解决方案,假设它也应该在silverlight中工作。它在 vb 中,但将其转换为 c# 应该不成问题。看看这里:http ://code.msdn.microsoft.com/Replace-numpad-decimalpoint-c1e0e6cd

于 2012-04-20T07:38:41.543 回答
0

您可以在 _LostFocus 中进行更改,无论是将点更改为逗号,还是将正确的文化应用于输入。

它不会按照您的意愿立即进行更改,但会在用户离开文本框后立即进行。

于 2008-12-04T09:24:47.087 回答
0

您可能会阅读“key up”事件而不是“key down”,并在每次按键时用(整个)首选替换字符串替换文本框的全部内容。

但是,我认为您的做法是错误的。键盘上有一个“,”键。我会假设如果您的用户打算输入“,”,因为这是他所在国家/地区的标准,那么这就是他会打的。屏幕上的键替换只会令人困惑。

于 2008-12-04T09:41:14.980 回答
0

使用 Alterlife 的答案作为替换内容的提示,我有以下工作技巧......但我不喜欢它:-(。

  • 这意味着Text属性被设置了两次,一次设置错误的值,然后替换为正确的值
  • 它仅适用于文本框
  • 感觉就像是某天可能会停止工作的黑客

因此,非常欢迎提出更好、更通用的解决方案的建议!

黑客:

    void CellText_KeyUp(object sender, KeyEventArgs e)
    {
        var DecSep = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator;

        if (e.Key == Key.Decimal && DecSep != ".")
        {
            if (e.OriginalSource is TextBox)
            {
                var TB = (TextBox)e.OriginalSource;
                string sText = TB.Text;

                int iPos = TB.SelectionStart - 1;
                if (iPos >= 0)
                {
                    System.Diagnostics.Debug.Assert(sText.Substring(iPos, 1) == ".");

                    TB.Text = sText.Substring(0, iPos) + DecSep + sText.Substring(iPos + 1);
                    TB.SelectionStart = iPos + 1; // reposition cursor
                }
            }
        }
    }
于 2008-12-04T10:32:19.980 回答
0

您正在尝试覆盖系统的语言环境设置,这可能会使您的用户在他们输入的键得到错误响应时感到困惑......您可以做的是编写一个值转换器,这将确保值正确在写入绑定数据字段之前形成。假设您的文本框绑定到基础数据字段...

于 2008-12-07T09:14:17.090 回答
0

它正在工作:

<html>
<heaD>
<script language="javascript">
function keypress1 ()
{
 var e=window.event || e
 unicode = e.charCode ? e.charCode : e.keyCode; 
 if (unicode==46)
   { return (e.charCode ? e.charCode=44 : e.keyCode=44); }
}
function keypress2 ()
{
 var e=window.event || e
 unicode = e.charCode ? e.charCode : e.keyCode; 
 if (unicode==46)
   { return (e.charCode ? e.charCode=46 : e.keyCode=46); }
}
function keyDown(e){
 if (!e){
   e = event
 }
 var code=e.keyCode;
 if(code==110)
   return document.onkeypress=keypress1
 else if(code=188)
   {  document.onkeypress=keypress2 }
}
document.onkeydown = keyDown
</script>
</head>
<body>
<input type=text>
</body>
</html>
于 2009-11-13T04:06:27.210 回答