在Silverlight's文本框中不能输入小数点
本文关键字:不能 输入 小数点 文本 Silverlight | 更新日期: 2023-09-27 18:12:39
我使用的是Silverlight 5。
我在文本框中输入小数点有问题。每次输入小数点时,光标总是返回到文本框中输入值的开头,小数点将被删除。
奇怪的是,这个问题只发生当我部署我的应用程序在IIS和运行它从互联网浏览器。当我在调试模式下从Visual Studio 2010运行它时,问题是不是。
下面是我的文本框的XAML代码:
<TextBox Grid.Column="2" Grid.Row="1" Text="{Binding Path=SelectedModel.MyHight,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Helper:TextBoxFilter.Filter="PositiveDecimal"/>
辅助类TextBoxFilter实际上在做PositiveDecimal过滤。下面是类(这些不是我的代码。从网上找到的):
TextBoxFilterType.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,
}
}
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 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;
//
}
}
}
我不明白为什么它在开发环境(调试)和实现环境(部署在IIS版本5.1)中的行为不同。
p。s:如果我删除UpdateSourceTrigger=PropertyChanged在文本属性,它会工作得很好。但是我需要这个属性来更新源而不丢失焦点。
在这里下载源代码
谢谢
请在MyModel类中将MyHeight属性更改为string而不是double。我以前遇到过类似的问题,我是这样解决的。是的,你必须在string和double之间进行不必要的装箱和拆箱。