0

I am not able to enter decimal values in DataGridTextColumn. Below link suggest that we cannot enter decimal values if UpdateSourceTrigger = PropertyChanged.

WPF DataGridTextColumn binding doesn't accept decimals

But my requirement is that I have to keep UpdateSourceTrigger = PropertyChanged.

Please suggest.

Below is the code snippet for my Datagrid:

  <DataGrid x:Name="MyDataGrid" HorizontalAlignment="Left" CanUserResizeColumns="True" VerticalAlignment="Stretch" ItemsSource="{Binding MyDataCollectionListView, Mode=TwoWay}"
      SelectionMode="Extended" IsSynchronizedWithCurrentItem="True" SelectionUnit="Cell">                                                      
     <DataGrid.Columns>
      <DataGridTextColumn x:Name="ColMyDataGrid" Header="Price" Binding="{Binding Path=Price, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource    ZeroToEmptyConverter}}" >
  <DataGridTextColumn.ElementStyle>
    <Style TargetType="TextBlock">
      <Setter Property="TextAlignment" Value="Right" />                                               
       <Setter Property="Background" Value="#BEFAC4"/>   
    </Style>
  </DataGridTextColumn.ElementStyle>
 </DataGridTextColumn>
</DataGrid.Columns>

Community
  • 1
  • 1
DevX
  • 725
  • 3
  • 13
  • 26

3 Answers3

1

UpdateSourceTrigger=LostFocus then allow remove decimal point automatically

Ghotekar Rahul
  • 322
  • 3
  • 10
0

You can code around the problem by adding a string property, 'PriceAsString', to the datagrid's ItemsSource model. Then convert the string to a decimal in the new property's setter and assign the value to Price. Then just bind the DataGridTextColumn to the PriceAsString Property. It's also necessary to add a conversion in the 'Price' property setter so that Price and PriceAsString are always synchronized.

The properties might look something like this

        private decimal? _price;
        public decimal? Price 
        { 
            get { return _price; } 
            set 
            {
                if (value != _price)
                {
                    _priceAsString = value.ToString();
                }

                _price = value;
            }
        }

        // Acts as a buffer for 'Price' so that the view doesn't remove decimals while typing in a control with 'UpdateSourceTrigger = PropertyChanged'
        private string _priceAsString;
        public string PriceAsString
        {
            get { return _priceAsString; }
            set
            {
                _priceAsString = value;
                Price = decimal.Parse(_priceAsString, CultureInfo.InvariantCulture);
            }
        }

Admittedly I'm not crazy about this solution since it adds bloat to the model and complicates things a bit. But if you absolutely need to use UpdateSourceTrigger=PropertyChanged, then maybe it's better than nothing...

If you do this you may also want to consider adding some text validation, since this will allow the text column to accept non-number/decimal characters.

Cams
  • 1
  • 1
0

How about adding Delay like 1 or 2 seconds in the Binding. This will delay the validation before it kicks off.

<DataGridTextColumn
 x:Name="ColMyDataGrid"
 Header="Price"
 Binding="{Binding Path=Price, Delay=1000,
           Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,
           Converter={StaticResource ZeroToEmptyConverter}}" >

It's annoying because the user must check the decimal point again while typing but good for temporary solution in my case.

Or, you can go to the WPF App constructor and add this code:

public App()
{
     FrameworkCompatibilityPreferences
          .KeepTextBoxDisplaySynchronizedWithTextProperty = false;
}
Riza Marhaban
  • 368
  • 6
  • 20