簡體   English   中英

在LostFocus上更新TextBox文本格式-WinRT

[英]update TextBox text format on LostFocus - WinRT

我正在使用C#和XAML開發Windows Store應用程序,並且正在用戶輸入頁面上工作。 我使用IValueConverter將綁定的數據轉換為貨幣格式的string而不是僅顯示小數。

當用戶導航到頁面時,轉換器可以正常工作,並且TextBox文本以貨幣格式顯示。 但是,當用戶更改TextBox.Text值,然后TextBox失去焦點時,轉換器將不會再次將其更改為一種不錯的貨幣格式,而只是保持用戶輸入時的格式。

據我所知,在WPF沒有像StringFormat那樣使用,那么在用戶編輯值之后,如何讓TextBox再次顯示貨幣格式?

我的轉換器:

public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (parameter == null)
            return ((decimal)value).ToString();
        return String.Format((string)parameter, (decimal)value);
    }

public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        decimal decVal = 0M;
        decimal.TryParse((string)value, out decVal);
        return decVal;
    }

我的XAML:

<Grid x:Name="InputGrid">
    ...
    <Border>
        <TextBox Text="{Binding MyValue, ConverterParameter='{}{0:c}', Converter={StaticResource DecimalValueConverter}, Mode=TwoWay}"
    </Border>
    ....
</Grid>

InputGrid.DataContext = MyValueClassInstance在代碼后面設置

如果您這樣做:

<TextBox Text="{Binding MyValue, UpdateSourceTrigger=PropertyChanged, ConverterParameter='{}{0:c}', Converter={StaticResource DecimalValueConverter}, Mode=TwoWay}"

轉換器可以雙向工作嗎? 我懷疑如果不這樣做-TwoWay綁定會損壞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM