【问题标题】:Cursor Goes Back To Start Why?光标返回开始 为什么?
【发布时间】:2013-10-04 07:54:09
【问题描述】:

我正在制作一个具有价格字段的 Windows Phone 应用程序。一旦用户开始输入我想更新一些其他的文本框。我使用的是 mvvm light,所以通常在用户离开文本框之前属性不会更新。

这对我不起作用,所以 I found this 并实现了它,但不是我有一个奇怪的问题,我不明白为什么。

当我在框 50 中键入时,属性首先更新为“5”,然后更新为“50”,这是预期的,但当我第一次输入“.”时什么都没有触发,然后当我输入 5 时,该属性似乎被点击了 3 次,一旦完成,它会将光标移回文本框的乞求处。

所以我得到 90.5 而不是 0.59

代码背后

private void txtPrice_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    TextBox textBox = sender as TextBox;
    textBox.Text = "";
}

private void txtPrice_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox textBox = sender as TextBox;
    // Update the binding source
    BindingExpression bindingExpr = textBox.GetBindingExpression(TextBox.TextProperty);
    bindingExpr.UpdateSource();
}

在模型中

 /// <summary>
    /// The <see cref="Price" /> property's name.
    /// </summary>
    public const string PricePropertyName = "Price";

    private decimal price = 0.00M;

    /// <summary>
    /// Sets and gets the Price property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public decimal Price
    {
        get
        {
            return price;
        }

        set
        {
            if (price == value)
            {
                return;
            }

            RaisePropertyChanging(() => Price);
            price = value;
            RaisePropertyChanged(() => Price);
        }
    }

XAML

<TextBox x:Name="txtPrice" Margin="157,16,102,0" TextWrapping="Wrap" VerticalAlignment="Top" InputScope="Number" Text="{Binding WeightConversion.Price, Mode=TwoWay, UpdateSourceTrigger=Explicit}" Tap="txtPrice_Tap" TextChanged="txtPrice_TextChanged" />

【问题讨论】:

    标签: c# mvvm windows-phone mvvm-light


    【解决方案1】:

    问题在于“。”本身不是有效的小数,因此当绑定尝试更新价格时,它会失败。稍后,当您输入“.5”时,它认为它是一个有效数字并更新 Price 的值,但是当价格提升属性发生变化并将其转换回 Text 时,它会转换为 0.5,这会强制文本框的“程序化”更新和会将光标重置到第一个位置。

    要解决这个问题,我看到的最佳解决方案可能是使用字符串属性来备份价格并“手动”更新十进制值:

    private decimal price = 0.00M;
    
    /// <summary>
    /// Sets and gets the Price property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public decimal Price
    {
        get
        {
            return price;
        }
    
        set
        {
            if (price == value)
            {
                return;
            }
    
            RaisePropertyChanging(() => Price);
            price = value;
            RaisePropertyChanged(() => Price);
    
    
            this.PriceStr = this.Price.ToString();
    
        }
    }
    
    
    private string priceStr=0.00M.ToString();
    /// <summary>
    /// Sets and gets the Price property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public string PriceStr
    {
        get
        {
            return priceStr;
        }
    
        set
        {
            if (priceStr == value)
            {
                return;
            }
    
    
            priceStr = value;
    
            isPriceAValidStr=decimal.TryParse(this.PriceStr, out price);
    
    
            RaisePropertyChanged(() => Price);
            RaisePropertyChanged(() => PriceStr);
        }
    }
    
            private bool isPriceAValidStr = true;
    

    并将 Text 的绑定更改为 PriceStr。

    还有另一个问题,即使使用InputScope="Number",仍然有一些方法可以在文本框中输入文本:

    • 通过使用具有一个的手机的硬件键盘(在模拟器中,您可以通过按向下翻页键来模拟,然后您将能够使用键盘输入文本)。要解决这个问题,您可以注册 key_down 事件并对e.Key 进行一些条件检查,并通过设置e.Handled = true; 拒绝所有您不想要的键。您也可以使用它来防止用户输入两次.
    • 通过从另一个文本框中复制一些文本来启用文本(可能还应该删除 TextChanged 中的所有无效字母)

    【讨论】:

    • 不知道有些设备有硬件键盘。然后我会这样做,我想知道我是否需要在手机上检查这些东西,就像你对网站所做的那样。
    • 我还没有看到任何带有硬件键盘的 windows phone 8,但它们是几个带有一个的 windows phone 7。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 2019-11-21
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    相关资源
    最近更新 更多