title: How to handle percent input and display in WPF?
body: <p>I have been looking for a good way to handle percentages. In our application we need to display and edit percent values, with a different number of decimal digits, i.e. when the user is not currently focusing on the textbox it should display 12.50%, but when editing the value it should be displayed as 0.12501015512312311. To make matters worse the user should be able to choose the number of decimals to displayed at runtime. Furthermore, the user should be allowed to input 12.5% which would translate to 0.125. I know I can do this with a converter, but I am having trouble with combining all of these requirements into a decent solution.</p>
  
  <p>Here is my best attempt thus far:</p>
  
  <pre><code>public class PercentConverter : IValueConverter
  {
      private static int numberOfDecimals = CultureInfo.CurrentCulture.NumberFormat.PercentDecimalDigits;
      public static int NumberOfDecimals
      {
          get { return numberOfDecimals; }
          set
          {
              if(value != numberOfDecimals)
              {
                  numberOfDecimals = value;
              }
          }
      }
  
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
          decimal pct = System.Convert.ToDecimal(value);
          CultureInfo ci = new CultureInfo(CultureInfo.CurrentCulture.Name, false);
          ci.NumberFormat.PercentDecimalDigits = NumberOfDecimals;
          return String.Format(ci, "{0:P}", pct);
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
          string percentString = value as string;
          string doubleString = percentString.TrimEnd(culture.NumberFormat.PercentSymbol.ToCharArray());
          double percent = Double.Parse(doubleString, NumberStyles.Any);
  
          if (IsExplicitPercentage(percentString, culture))
          {
              percent /= 100;
          }
  
          return percent;
      }
  
      private bool IsExplicitPercentage(string percentString, CultureInfo culture)
      {
          return percentString.Contains(culture.NumberFormat.PercentSymbol);
      }
  }
  </code></pre>
  
  <p>and then in xaml</p>
  
  <pre><code>&lt;DataGrid.Columns&gt;
              &lt;DataGridTextColumn Header="Beskrivelse" Binding="{Binding Description}" /&gt;
              &lt;DataGridTemplateColumn Header="Share"&gt;
                  &lt;DataGridTemplateColumn.CellTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;TextBox HorizontalContentAlignment="Right" HorizontalAlignment="Stretch"&gt;
                              &lt;TextBox.Style&gt;
                                  &lt;Style TargetType="{x:Type TextBox}"&gt;
                                      &lt;!-- Other Style Setters --&gt;
                                      &lt;Setter Property="Text" Value="{Binding Share, Converter={StaticResource pctConverter}}" /&gt;
                                      &lt;Style.Triggers&gt;
                                          &lt;Trigger Property="IsKeyboardFocused" Value="True"&gt;
                                              &lt;Setter Property="Text" Value="{Binding Share}" /&gt;
                                          &lt;/Trigger&gt;
                                      &lt;/Style.Triggers&gt;
                                  &lt;/Style&gt;
                              &lt;/TextBox.Style&gt;
                          &lt;/TextBox&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellTemplate&gt;
              &lt;/DataGridTemplateColumn&gt;
          &lt;/DataGrid.Columns&gt;
  </code></pre>
  
  <p>But this will not bind properly to the datacontex, when i change a value in the textbox, it does not update the property on the model it is bound to. </p>
  
  <p>Note that I am pretty new to WPF, so I could be missing something basic.</p>