1

Silverlight5 を使用しています。

TextBox に小数点を入力するのに問題があります。小数点を入力するたびに、カーソルは常に TextBox に入力された値の先頭に戻り、小数点が削除されます。

奇妙なことに、この問題は、IIS にアプリケーションを展開し、インターネット ブラウザーから実行した場合にのみ発生します。Visual Studio 2010 からデバッグ モードで実行すると、問題は発生しませんでした。

以下は、TextBox の 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 に展開) で動作が異なる理由がわかりません。

ps: Text プロパティでUpdateSourceTrigger=PropertyChangedを削除すると、正常に動作します。しかし、フォーカスを失わずにソースを更新するには、そのプロパティが必要です。

ソースコードのダウンロードはこちら

ありがとうございました

4

1 に答える 1

0

MyModelクラスのMyHeightプロパティをdoubleではなくstringに変更してください。私は以前に同様の問題に直面し、この方法で解決しました。はい、ここで string と double の間で不要なボックス化とボックス化解除を行う必要がありますが、これは機能します。

于 2014-12-30T08:15:16.773 に答える