2017-09-04 1 views
0

J'ai un projet WPF avec un TextBlock avec et de liaison à un certain nombre comme "0009012" mais je veux montrer que 9012 ou pour 0881012 doivent se montrer 881012.TextBlock StringFormat Garniture Zéros

Existe-t-il un moyen avec StringFormat ou dois-je écrire un convertisseur pour cela?

Voici mon code:

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <TextBlock Text="{Binding Test}" StringFormat={}{????}/> 
</Grid> 
</Window> 
using System.Windows; 

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public string Test { get; set; } 

     public MainWindow() 
     { 
      Test = "00090012"; 

      InitializeComponent(); 
      DataContext = this; 
     } 
    } 
} 
+2

'" 00090012 "' n'est pas un nombre. c'est une chaîne. il est affiché tel quel. – ASh

+0

Pourriez-vous essayer 'StringFormat = {} {0: 0}}' 'et voir ce qui est affiché? –

+0

*" Y at-il un moyen avec StringFormat ou dois-je écrire un convertisseur pour cela? "* Vous devez écrire un convertisseur –

Répondre

-1
Test = Convert.ToInt32(Test).ToString(); 

Conversion en entier va supprimer tous les 0s initiale et vous pouvez le convertir en chaînes de caractères avec la méthode ToString.

+0

je dois le faire dans WPF/XAML désolé je ne l'ai pas précisé, parce que c'est un PrintLayout qui est chargé dans Runtime –

-2

int entier = int.Parse (String.ToString()); Essayez cette méthode

0

Est-il possible avec StringFormat ou dois-je écrire un Converter pour cela?

Ce dernier.

Il n'y a pas StringFormat vous pouvez appliquer en XAML qui supprime tous les zéros en tête d'un nombre. Notez également qu'un StringFormat n'est pas appliqué aux valeurs string.

Vous pouvez écrire un convertisseur qui appelle par exemple TrimStart("0") sur le string ou simplement renvoyer une valeur déjà tronquée de la propriété source du modèle de vue auquel vous liez.