TextBox n'honore pas le système décimal (point ou virgule)

Si je bind Text dans un TextBox à une propriété float alors le texte affiché n'honore pas la décimale du système (point ou virgule). Au lieu de cela, il affiche toujours un point (".'). Mais si j'affiche la valeur dans un MessageBox (en utilisant ToString ()) alors la décimale correcte du système est utilisée.

enter image description here

Xaml

<StackPanel>
    <TextBox Name="floatTextBox"
             Text="{Binding FloatValue}"
             Width="75"
             Height="23"
             HorizontalAlignment="Left"/>
    <Button Name="displayValueButton"
            Content="Display value"
            Width="75"
            Height="23"
            HorizontalAlignment="Left"
            Click="displayValueButton_Click"/>
</StackPanel>

Code derrière

public MainWindow()
{
    InitializeComponent();
    FloatValue = 1.234f;
    this.DataContext = this;
}
public float FloatValue
{
    get;
    set;
}
private void displayValueButton_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show(FloatValue.ToString());
}

maintenant, j'ai résolu ce problème avec un Convertisseur qui remplace point avec le système décimal (qui fonctionne) mais quelle est la raison pour laquelle c'est nécessaire? Est-ce par sa conception et est-il un moyen plus facile de résoudre ce problème?

SystemDecimalConverter (au cas où quelqu'un d'autre a le même problème)

public class SystemDecimalConverter : IValueConverter
{
    private char m_systemDecimal = '#';
    public SystemDecimalConverter()
    {
        m_systemDecimal = GetSystemDecimal();
    }
    object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value.ToString().Replace('.', m_systemDecimal);
    }
    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value.ToString().Replace(m_systemDecimal, '.');
    }
    public static char GetSystemDecimal()
    {
        return string.Format("{0}", 1.1f)[1];
    }
}
12
demandé sur Fredrik Hedblad 2011-01-27 14:32:06

1 réponses