Is there any way to execute Textbox UpdateSourceTrigger=LostFocus before Lost Focus Event?

433 Views Asked by At

I have a TextBox of which the Text property must be updated before executing the LostFocus event, because in the LostFocus event I use the values of the Text property for calculations.

Currently when entering the LostFocus event (txtTotaisNfe_LostFocus) the ThisDataContext.DescontoTotal has not yet had its value updated.

P.S: I cannot use UpdateSourceTrigger = PropertyChanged because I use a converter (PriceConverter1) in WPF that can only be executed after I have entered the complete value in the TextBox. It converts the entered value into 2 decimal cases value (that is, when running LostFocus).

The question is: Is there any way in the .NET process queue to understand that it must first run the LostFocus from UpdateSourceTrigger and then the LostFocus (txtTotaisNfe_LostFocus)?

WPF Code (DescontoTotal = ThisDataContext.Desconto):

<TextBox Text="{Binding DescontoTotal, Converter={StaticResource PriceConverter1}, Mode=TwoWay, UpdateSourceTrigger=Explicit}" LostFocus="txtTotaisNfe_LostFocus"/>

LostFocus Event Code:

private void txtTotaisNfe_LostFocus(object sender, RoutedEventArgs e)
{
    //Rateia o Total de Desconto da NFe entre os Produtos.
    foreach (var item in _lsProdutos)
    {
        item.Desconto = (ThisDataContext.DescontoTotal / ThisDataContext.TotalProdutos * item.ValorProduto).Round();
    }
}

PriceConverter1 Code (Please ignore Best Practices in this):

public class PriceConverter1 : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                try
                {
                    return ((decimal)value).ToString("##,###,##0.00");
                }
                catch
                {
                    return "0.00";
                }
            }
            else
            {
                return "0.00";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string price = value.ToString();

            decimal result;
            if (decimal.TryParse(price, System.Globalization.NumberStyles.Any, null, out result))
            {
                return result;
            }
            return value;
        }
    }
0

There are 0 best solutions below