当用户在文本框中时(我在Silverlight 2.0中)尝试做什么:
我可以通过检入keydown事件跟踪用户输入小数点
void Cell_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Decimal)
但是如何在Silverlight中用其他键替换该键。 e.Key
是只读的。有没有办法向控件发送“其他密钥”?还是其他任何建议?
答案 0 :(得分: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
答案 1 :(得分: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);
}
}
干杯
答案 2 :(得分:1)
我有一个解决方案,我在winforms应用程序中使用,假设它也应该在silverlight中工作。它在vb中,但将其转换为c#应该不是问题。 看看这里:http://code.msdn.microsoft.com/Replace-numpad-decimalpoint-c1e0e6cd
答案 3 :(得分:0)
您可以在_LostFocus中进行更改,将点更改为逗号,或将正确的文化应用于输入。
它不会立即按照您的意愿进行更改,但会在用户离开文本框后立即执行此操作。
答案 4 :(得分:0)
你可能会读取'key up'事件而不是'key down',并在每个按键上用你的(整个)首选替换字符串替换文本框的整个内容。
然而,我认为你的做法是错误的。键盘上有一个“,”键。我会假设,如果你的用户打算输入','因为这是他所在国家的标准,那么他就会打到它。屏幕上的键替换会让人感到困惑。
答案 5 :(得分:0)
使用Alterlife的答案作为替换内容的提示,我有以下工作黑客...但我不喜欢它: - (。
因此非常欢迎有关更好,更通用的解决方案的建议!
黑客:
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
}
}
}
}
答案 6 :(得分:0)
您正试图覆盖系统的区域设置,当用户从他们输入的密钥获得错误响应时,这可能会让用户感到困惑...... 你可以做的是写一个值转换器,它可以确保在写入绑定数据字段之前正确形成值。假设您的文本框绑定到基础数据字段...
答案 7 :(得分: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>