C# 无法在Silverlight'中输入小数点;s文本框
我用的是Silverlight 5 我在文本框中输入小数点时遇到问题。每次输入小数点时,光标将始终返回文本框中输入值的开头,小数点将被删除 奇怪的是,只有当我在IIS上部署应用程序并从internet浏览器运行它时,才会出现这个问题。当我在调试模式下从VisualStudio2010运行它时,问题不在那里 下面是文本框的XAML代码:C# 无法在Silverlight'中输入小数点;s文本框,c#,silverlight,C#,Silverlight,我用的是Silverlight 5 我在文本框中输入小数点时遇到问题。每次输入小数点时,光标将始终返回文本框中输入值的开头,小数点将被删除 奇怪的是,只有当我在IIS上部署应用程序并从internet浏览器运行它时,才会出现这个问题。当我在调试模式下从VisualStudio2010运行它时,问题不在那里 下面是文本框的XAML代码: <TextBox Grid.Column="2" Grid.Row="1" Text="{Binding Path=SelectedModel.MyHigh
<TextBox Grid.Column="2" Grid.Row="1" Text="{Binding Path=SelectedModel.MyHight,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Helper:TextBoxFilter.Filter="PositiveDecimal"/>
TextBoxFilter.cs
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace NumericUpdateSourceProblem
{
public enum TextBoxFilterType
{
None,
PositiveInteger,
Integer,
PositiveDecimal,
Decimal,
Alpha,
}
}
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace NumericUpdateSourceProblem
{
public class TextBoxFilter
{
/// <summary>
/// Filter Attached Dependency Property
/// </summary>
public static readonly DependencyProperty FilterProperty =
DependencyProperty.RegisterAttached("Filter", typeof(TextBoxFilterType), typeof(TextBoxFilter),
new PropertyMetadata(OnFilterChanged));
/// <summary>
/// Gets the Filter property.
/// </summary>
public static TextBoxFilterType GetFilter(DependencyObject d)
{
return (TextBoxFilterType)d.GetValue(FilterProperty);
}
/// <summary>
/// Sets the Filter property.
/// </summary>
public static void SetFilter(DependencyObject d, TextBoxFilterType value)
{
d.SetValue(FilterProperty, value);
}
/// <summary>
/// Handles changes to the Filter property.
/// </summary>
/// <param name="d">DependencyObject</param>
/// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void OnFilterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextBox textBox = d as TextBox;
if (TextBoxFilterType.None != (TextBoxFilterType)e.OldValue)
{
textBox.KeyDown -= new KeyEventHandler(textBox_KeyDown);
}
if (TextBoxFilterType.None != (TextBoxFilterType)e.NewValue)
{
textBox.KeyDown += new KeyEventHandler(textBox_KeyDown);
}
}
/// <summary>
/// Handles the KeyDown event of the textBox control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param>
private static void textBox_KeyDown(object sender, KeyEventArgs e)
{
// bypass other keys!
if (IsValidOtherKey(e.Key))
{
return;
}
//
TextBoxFilterType filterType = GetFilter((DependencyObject)sender);
TextBox textBox = sender as TextBox;
if (null == textBox)
{
textBox = e.OriginalSource as TextBox;
}
//
switch (filterType)
{
case TextBoxFilterType.PositiveInteger:
e.Handled = !IsValidIntegerKey(textBox, e.Key, e.PlatformKeyCode, false);
break;
case TextBoxFilterType.Integer:
e.Handled = !IsValidIntegerKey(textBox, e.Key, e.PlatformKeyCode, true);
break;
ase TextBoxFilterType.PositiveDecimal:
e.Handled = !IsValidDecmialKey(textBox, e.Key, e.PlatformKeyCode, false);
break;
case TextBoxFilterType.Decimal:
e.Handled = !IsValidDecmialKey(textBox, e.Key, e.PlatformKeyCode, true);
break;
case TextBoxFilterType.Alpha:
e.Handled = !IsValidAlphaKey(e.Key);
break;
}
}
/// <summary>
/// Determines whether the specified key is valid other key.
/// </summary>
/// <param name="key">The key.</param>
/// <returns>
/// <c>true</c> if [is valid other key] [the specified key]; otherwise, <c>false</c>.
/// </returns>
private static bool IsValidOtherKey(Key key)
{
// allow control keys
if ((Keyboard.Modifiers & ModifierKeys.Control) != 0)
{
return true;
}
// allow
// Back, Tab, Enter, Shift, Ctrl, Alt, CapsLock, Escape, PageUp, PageDown
// End, Home, Left, Up, Right, Down, Insert, Delete
// except for space!
// allow all Fx keys
if (
(key < Key.D0 && key != Key.Space)
|| (key > Key.Z && key < Key.NumPad0))
{
return true;
}
// we need to examine all others!
return false;
}
/// <summary>
/// Determines whether the specified key is valid integer key for the specified text box.
/// </summary>
/// <param name="textBox">The text box.</param>
/// <param name="key">The key.</param>
/// <param name="platformKeyCode">The platform key code.</param>
/// <param name="negativeAllowed">if set to <c>true</c> [negative allowed].</param>
/// <returns>
/// <c>true</c> if the specified key is valid integer key for the specified text box; otherwise, <c>false</c>.
/// </returns>
private static bool IsValidIntegerKey(TextBox textBox, Key key, int platformKeyCode, bool negativeAllowed)
{
if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0)
{
return false;
}
if (Key.D0 <= key && key <= Key.D9)
{
return true;
}
if (Key.NumPad0 <= key && key <= Key.NumPad9)
{
return true;
}
if (negativeAllowed && (key == Key.Subtract || (key == Key.Unknown && platformKeyCode == 189)))
{
return 0 == textBox.Text.Length;
}
//
return false;
}
/// <summary>
/// Determines whether the specified key is valid decmial key for the specified text box.
/// </summary>
/// <param name="textBox">The text box.</param>
/// <param name="key">The key.</param>
/// <param name="platformKeyCode">The platform key code.</param>
/// <param name="negativeAllowed">if set to <c>true</c> [negative allowed].</param>
/// <returns>
/// <c>true</c> if the specified key is valid decmial key for the specified text box; otherwise, <c>false</c>.
/// </returns>
private static bool IsValidDecmialKey(TextBox textBox, Key key, int platformKeyCode, bool negativeAllowed)
{
if (IsValidIntegerKey(textBox, key, platformKeyCode, negativeAllowed))
{
return true;
}
if (key == Key.Decimal || (key == Key.Unknown && platformKeyCode == 190))
{
return !textBox.Text.Contains(".");
}
return false;
//
}
/// <summary>
/// Determines whether the specified key is valid alpha key.
/// </summary>
/// <param name="key">The key.</param>
/// <returns>
/// <c>true</c> if the specified key is valid alpha key for the specified text box; otherwise, <c>false</c>.
/// </returns>
private static bool IsValidAlphaKey(Key key)
{
if (Key.A <= key && key <= Key.Z)
{
return true;
}
//
return false;
//
}
}
}
使用系统;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Documents;
使用System.Windows.Ink;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Animation;
使用System.Windows.Shapes;
命名空间NumericUpdateSourceProblem
{
公共类TextBoxFilter
{
///
///筛选器附加的依赖项属性
///
公共静态只读从属属性FilterProperty=
DependencyProperty.RegisterAttached(“过滤器”、类型(TextBoxFilterType)、类型(TextBoxFilter),
新属性元数据(已更改);
///
///获取筛选器属性。
///
公共静态TextBoxFilterType GetFilter(DependencyObject d)
{
返回(TextBoxFilterType)d.GetValue(FilterProperty);
}
///
///设置过滤器属性。
///
公共静态void SetFilter(DependencyObject d,TextBoxFilterType值)
{
d、 设置值(过滤器属性,值);
}
///
///处理对过滤器属性的更改。
///
///从属对象
///包含事件数据的实例。
私有静态无效OnFilterChanged(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
TextBox TextBox=d作为TextBox;
if(TextBoxFilterType.None!=(TextBoxFilterType)e.OldValue)
{
textBox.KeyDown-=新的KeyEventHandler(textBox\u KeyDown);
}
if(TextBoxFilterType.None!=(TextBoxFilterType)e.NewValue)
{
textBox.KeyDown+=新的KeyEventHandler(textBox\u KeyDown);
}
}
///
///处理textBox控件的KeyDown事件。
///
///事件的来源。
///包含事件数据的实例。
私有静态无效文本框\u KeyDown(对象发送方,KeyEventArgs e)
{
//绕过其他键!
如果(IsValidOtherKey(e.Key))
{
返回;
}
//
TextBoxFilterType filterType=GetFilter((DependencyObject)发送方);
TextBox TextBox=发送者作为TextBox;
如果(空==文本框)
{
textBox=e.OriginalSource作为textBox;
}
//
开关(过滤器类型)
{
案例TextBoxFilterType.PositiveInteger:
e、 Handled=!IsValidIntegerKey(文本框,e.键,e.平台键码,false);
打破
案例TextBoxFilterType.Integer:
e、 Handled=!IsValidIntegerKey(文本框,e.键,e.平台键,true);
打破
ase TextBoxFilterType.PositiveDecimal:
e、 Handled=!IsValidDecMilkey(文本框,e.键,e.平台键代码,false);
打破
案例TextBoxFilterType.Decimal:
e、 Handled=!IsValidDecMilkey(文本框,e.键,e.平台键代码,真);
打破
案例TextBoxFilterType.Alpha:
e、 已处理=!IsValidAlphaKey(e.Key);
打破
}
}
///
///确定指定的键是否为有效的其他键。
///
///钥匙。
///
///如果[是有效的其他键][指定的键],则为true;否则为false。
///
专用静态bool IsValidOtherKey(Key)
{
//允许控制键
if((Keyboard.Modifiers和ModifierKeys.Control)!=0)
{
返回true;
}
//容许
//后退、制表符、回车、Shift、Ctrl、Alt、CapsLock、Escape、PageUp、PageDown
//结束、起始、左、上、右、下、插入、删除
//除了空间!
//允许所有的外汇关键点
如果(
(keykey.Z&&key 如果(Key.D0请将MyHeight属性更改为string,而不是MyModel类中的double。我以前也遇到过类似的问题,并通过这种方式解决了。是的,您必须在这里对string和double进行不必要的装箱和拆箱操作。但是这是有效的。中的是有效的y
函数尝试替换return!textBox.Text.Contains(“.”;
by