3

All I really want to do is change the StringFormat for a particular binding on a DataGridTextColumn, but since that is defined by the binding, I suppose that I need to change the entire binding somehow.

The reason that I want to avoid using a DataGridTemplateColumn is because the DataGridTextColumn seems to have some built-in base functionality that I would lose and have to write code to handle more manually, such as:

  • The ability to start typing into a non-edit mode cell which puts the cell automatically into edit mode
  • Copying and pasting of data (from within the DataGrid and from Excel)

Current Binding (Desired Display/Read Mode Binding)

Binding="{Binding ADecimalNumber StringFormat={StaticResource ReadDecimalFormat}}"

Desired Edit Mode Binding

Binding="{Binding ADecimalNumber StringFormat={StaticResource WriteDecimalFormat}}"

Just as an FYI, the ReadDecimalFormat is to display the decimal value with 2-digits of precision, and the WriteDecimalFormat is to display the original decimal value entered (all-digits of precision, most cases this is 4-digits)

Jon Erickson
  • 112,242
  • 44
  • 136
  • 174

2 Answers2

4

I think I've tentatively gotten it working, currently testing it.

<DataGridTextColumn Header="Value" ClipboardContentBinding="{Binding ADecimalNumber}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="TextBlock">
            <Setter Property="Text" Value="{Binding ADecimalNumber, StringFormat={StaticResource ReadDecimalFormat}}"/>
        </Style>
    </DataGridTextColumn.ElementStyle>
    <DataGridTextColumn.EditingElementStyle>
        <Style TargetType="TextBox">
            <Setter Property="Text" Value="{Binding ADecimalNumber, StringFormat={StaticResource WriteDecimalFormat}}"/>
        </Style>
    </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
Jon Erickson
  • 112,242
  • 44
  • 136
  • 174
  • It looks like the automatic handling for copying and pasting no longer works with this solution, but the F2 to edit and type to edit mode is still working – Jon Erickson Sep 19 '12 at 19:32
  • Take that back, copy/paste is working fine, just need to set the ClipboardContentBinding to the correct binding eg. "{Binding ADecimalNumber}", I'm assuming that the ClipboardContentBinding defaults to the Binding if it is set on the DataGridTextColumn element, which is no longer the case in this solution, therefore we must manually set it – Jon Erickson Sep 19 '12 at 20:49
2

I was able to accomplish this using element styles and checking for whether we were editing or not:

<DataGridTextColumn>
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Text" Value="{Binding}" />
        </Style>
    </DataGridTextColumn.ElementStyle>


<DataGridTextColumn.EditingElementStyle>
    <Style TargetType="{x:Type TextBox}">
      <Setter Property="Text" Value="{Binding}" />
     </Style>
 </DataGridTextColumn.EditingElementStyle>

Josh
  • 10,352
  • 12
  • 58
  • 109