Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to multibind data and StringFormat in a datagrid column?
    text
    copied!<p>I have a WPF <code>DataGrid</code> with numeric columns that are initially formatted without decimals.</p> <p>The user has a checkbox to select the numeric format to show 0 or 2 decimal places. Below is shown the xaml for the column. </p> <pre><code>&lt;DataGridTemplateColumn Header="Qty" Width="40" IsReadOnly="False" CellStyle="{StaticResource EditCell}"&gt; &lt;DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding ItemQuantity, StringFormat={}{0:#}}" TextAlignment="Right" /&gt; &lt;/DataTemplate&gt; &lt;/DataGridTemplateColumn.CellTemplate&gt; &lt;DataGridTemplateColumn.CellEditingTemplate&gt; &lt;DataTemplate&gt; &lt;TextBox x:Name="textbox" BorderThickness="0" HorizontalContentAlignment="Right" Background="LightYellow"&gt; &lt;Binding Path="ItemQuantity" StringFormat="N0" UpdateSourceTrigger="LostFocus" &gt; &lt;Binding.ValidationRules&gt; &lt;c:DecimalRangeRule Min="0" Max="999999.99"/&gt; &lt;/Binding.ValidationRules&gt; &lt;/Binding&gt; &lt;/TextBox&gt; &lt;/DataTemplate&gt; &lt;/DataGridTemplateColumn.CellEditingTemplate&gt; &lt;/DataGridTemplateColumn&gt; </code></pre> <p>How can I change the column <code>StringFormat</code> setting when the checkbox is changed?</p> <p><strong>EDIT: Solution</strong></p> <p>I used a MultiConverter (as suggested by nit) to select the format based on whether the checkbox is checked:</p> <pre><code>//========================================================================== public class NumericFormatConverter : IMultiValueConverter { private const string FormatN0 = "{0:#,##0;-#,##0; }"; private const string FormatN2 = "{0:#,##0.00;-#,##0.00; }"; public object Convert( object[] values, Type targetType, object parameter, CultureInfo culture ) { if ( values[0] == null ) return string.Empty; decimal num = 0; Decimal.TryParse( values[0].ToString(), out num ); string format = FormatN0; bool isSmallValue = (values[1] == null ? false : (bool)values[1]); if ( isSmallValue ) format = FormatN2; return String.Format( format, num ); } public object[] ConvertBack( object value, Type[] targetTypes, object parameter, CultureInfo culture ) { decimal num = 0; Decimal.TryParse( value.ToString(), out num ); object[] objects = new object[1] {num}; return objects; } } </code></pre> <p>The DataGridTemplateColumn changed to use MultiBinding:</p> <pre><code>&lt;DataGridTemplateColumn Header="Qty" Width="40" IsReadOnly="False" CellStyle="{StaticResource EditCell}"&gt; &lt;DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock HorizontalAlignment="Right" &gt; &lt;TextBlock.Text&gt; &lt;MultiBinding Converter="{StaticResource NumericFormat}"&gt; &lt;Binding Path="GroupQuantity" /&gt; &lt;Binding ElementName="chkSmallValue" Path="IsChecked"/&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; &lt;/DataTemplate&gt; &lt;/DataGridTemplateColumn.CellTemplate&gt; &lt;DataGridTemplateColumn.CellEditingTemplate&gt; &lt;DataTemplate&gt; &lt;TextBox x:Name="textbox" BorderThickness="0" HorizontalContentAlignment="Right" Background="LemonChiffon" PreviewTextInput="TextBox_PreviewTextInput" &gt; &lt;MultiBinding Converter="{StaticResource NumericFormat}" UpdateSourceTrigger="LostFocus"&gt; &lt;Binding Path="GroupQuantity" /&gt; &lt;Binding ElementName="chkSmallValue" Path="IsChecked" Mode="OneWay"/&gt; &lt;/MultiBinding&gt; &lt;/TextBox&gt; &lt;/DataTemplate&gt; &lt;/DataGridTemplateColumn.CellEditingTemplate&gt; &lt;/DataGridTemplateColumn&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload