Search code examples
c#wpfxamldata-bindingdatagrid

WPF: UpdateSourceTrigger=PropertyChanged issue with Double Values


I'm working on a WPF project where I have a DataGrid bounded to a ObservableCollection. The values are being bounded properly but the issue that I am running into is that I can't edit the columns with double values. It will not let me insert a period into the cell.

This is what I have for the XAML:

<DataGrid Name="dataGrid" AutoGenerateColumns="False" 
              CanUserResizeColumns="True" CanUserAddRows="False" CanUserSortColumns="True" ItemsSource="{Binding}"
              HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" 
              ColumnWidth="*" Margin="0,51,186,58" 
              RowEditEnding="dataGrid_RowEditEnding">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Field 1" Binding="{Binding Field1, UpdateSourceTrigger=PropertyChanged}" />
            <DataGridTextColumn Header="Field 2" Binding="{Binding Field2, UpdateSourceTrigger=PropertyChanged}" />
            <DataGridTextColumn Header="Field 3" Binding="{Binding Field3, UpdateSourceTrigger=PropertyChanged}" />
            <DataGridTextColumn Header="Field 4" Binding="{Binding Field4, UpdateSourceTrigger=PropertyChanged}" />
            <DataGridCheckBoxColumn Header="Field 5" Binding="{Binding Field5, UpdateSourceTrigger=PropertyChanged}" />
            <DataGridTextColumn Header="Field 6" Binding="{Binding Field6, UpdateSourceTrigger=PropertyChanged}" />
        </DataGrid.Columns>

<DataGrid>

And here is the class (Sorry for the weird class properties :/ I made it that way on purpose)

class FieldClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private int _field1;
    public int Field1
    {
        get { return _field1; }
        set 
        {
            _field1 = value;
            OnPropertyChanged("Field1");
        }
    }

    private int _field2;
    public int Field2
    {
        get { return _field2; }
        set 
        {
            _field2 = value;
            OnPropertyChanged("Field2");
        }
    }

    private double _field3;
    public double Field3
    {
        get { return _field3; }
        set
        {
            _field3 = value;
            OnPropertyChanged("Field3");
        }
    }

    private double _field4;
    public double Field4
    {
        get { return _field4; }
        set 
        {
            _field4 = value;
            OnPropertyChanged("Field4");
        }
    }

    private bool _field5;
    public bool Field5
    {
        get { return _field5; }
        set 
        {
            _field5 = value;
            OnPropertyChanged("Field5");
        }
    }

    private double _field6;
    public double Field6
    {
        get { return _field6; }
        set 
        {
            _field6 = value;
            OnPropertyChanged("Field6");
        }
    }

    public FieldClass()
    {
        _field1 = 0;
        _field2 = 0;
        _field3 = 0.0;
        _field4 = 0.0;
        _field5 = false;
        _field6 = 0.0;
    }

    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

How can I make it so on the DataGrid, if I wanted to update the value of a column (lets say i want to update Field3 or any field that has a double), I can insert a double value like 2.1?

Is a data template needed? Not to sure how to go about that, still a beginner.

Thanks for the help!


Solution

  • If you really want to obtain the behavior with PropertyChanged trigger you can try to use IsAsync=true of Binding, but I'm not sure that's the correct solution.

    <DataGridTextColumn Header="Field 3" Binding="{Binding Field3, UpdateSourceTrigger=PropertyChanged, StringFormat=\{0:n\}, IsAsync=True}" />