1

我正在使用 Silverlight 中的绑定。我已将 TextBox 与 Decimal 实体绑定。下面是绑定的代码片段。

<TextBox x:Name="AmountBox" Text="{Binding SelectedEntity.Amount,Mode=TwoWay,StringFormat=\{0:n2\},Converter={StaticResource DecimalBlankValueConverter}}" Validate="True" TextChanged="AmountBox_TextChanged" LostFocus="AmountBox_LostFocus"/>

下面是转换器代码。

    decimal result;
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {            
        if (!Decimal.TryParse(value.ToString(),out result) || (decimal)value == decimal.Zero)
            return null;
        return decimal.Parse(value.ToString());
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null || !Decimal.TryParse(value.ToString(), out result))
            return 0.00;
        return decimal.Parse(value.ToString());
    }

在失去焦点时,我正在使用 GetBindingExpression(TextBox.TextProperty).UpdateSource();

一切都很好,但是在失去焦点时不会调用转换,当我在文本框中输入字符串时,不会调用转换,它不会将文本框文本转换为空白。

谁能建议我,代码中的问题是什么。

提前致谢。----拉杰

4

3 回答 3

0

根据您的描述,“在失去焦点时不会调用转换,当我在文本框中输入字符串时,不会调用转换,它不会将文本框文本转换为空白。”

在这里,只有在与 TextBox 的 Text 属性的绑定发生变化时才会调用 Convert 方法。因为,您已经提到了 Mode=TwoWay 进行绑定,所以当您在文本框中输入任何文本时,将调用ConvertBack 方法,并且从此方法返回的值将分配给 Source,在您的情况下,它是 SelectedEntity.Amount。

我不明白为什么我们需要显式编写 GetBindingExpression(TextBox.TextProperty).UpdateSource(); 失去焦点的代码更新源代码。

理想情况下,由于您保留了绑定模式两种方式,它应该在调用 Converter 的 ConvertBack 方法后更新源。转换器代码对我来说看起来不错。

如果您需要更多详细信息,或者我可能误解了某些内容,请告诉我,请澄清这些观点。

于 2012-08-22T13:21:59.233 回答
0

如果您手动更新 TextChanged 事件中的绑定,如下所示:

GetBindingExpression(TextBox.TextProperty).UpdateSource();

也许您将在 LostFocus 事件中获得的值与您在 TextChanged 事件中更新的值没有什么不同!

所以,很可能转换器不会被调用,只是因为绑定中的值没有改变!

希望这可以帮助!

于 2012-08-22T13:26:44.497 回答
0

TwoWayTextChanged与事件绑定的模式LostFocus绝对不是最好的方法。为什么默认调用GetBindingExpression(TextBox.TextProperty).UpdateSource();ifLostFocus绑定?如果要手动更新绑定,请设置UpdateSourceTrigger=Explicit.

无论如何,您的转换器看起来不错,但我认为您不需要它。如果我理解正确,如果无法将文本转换为十进制,则要清除文本。在这种情况下,您有几个选项,例如创建TextBox只允许数字的自定义,或者您可以从Silverlight ToolkitNumericUpDown检查控件。我也在这里找到了类似的问题。

如果您已经安装Microsoft Expression Blend(如果没有,您可以BlendSLSDK_en.msiMicrosoft Expression Blend Software Development Kit下载),将System.Windows.Interactivitydll 添加到您的项目中并Behavior像这样简单地创建:

public class TextBoxClearTextBehavior : System.Windows.Interactivity.Behavior<System.Windows.Controls.TextBox>
{
    protected override void OnAttached()
    {
        AssociatedObject.LostFocus += AssociatedObjectLostFocus;
        base.OnAttached();
    }

    protected override void OnDetaching()
    {
        AssociatedObject.LostFocus -= AssociatedObjectLostFocus;
        base.OnDetaching();
    }

    private void AssociatedObjectLostFocus(object sender, System.Windows.RoutedEventArgs e)
    {
        decimal result;
        if (!decimal.TryParse(AssociatedObject.Text, out result))
            AssociatedObject.Text = string.Empty;
    }
}

并像使用它一样

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

...    

<TextBox Text="{Binding Amount, Mode=TwoWay, StringFormat=\{0:n2\}}">
    <i:Interaction.Behaviors>
        <Behavior:TextBoxClearTextBehavior/>
    </i:Interaction.Behaviors>
</TextBox>
于 2012-08-22T14:16:40.567 回答