Yuki
Yuki

Reputation: 43

WPF DataGrid calculations

I am very new to WPF and C# so please help to solve this problem. I have a DataGrid, columns as follows: "One", "Two", "Multiply".

The idea that I am entering number "One" or "Two" and get result in the column "Multiply".

When I write my code and debug it, I can see that the value of the property is being re-calculated. However, I would not display in my last column unless I push space bar, or click on my last column.

Here is the code:

public class Numbers : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged; 

    private double _one;
    private double _two;
    private double _multiply;

    public double One
    {
        get { return _one; }
        set { _one = value; UpdateValue(); }
    }

    public double Two
    {
        get { return _two; }
        set { _two = value; UpdateValue(); }
    }

    public double Multiply
    {
        get { return _multiply; }
        set { _multiply = value; UpdateValue(); }
    }

    public Numbers(double pOne, double pTwo)
    {
        _one = pOne;
        _two = pTwo;
        _multiply = GetMultiply(); 
    }

    private void UpdateValue()
    {
        OnPropertyChanged("One");
        OnPropertyChanged("Two");
        OnPropertyChanged("Multiply");
        _multiply = GetMultiply();
    }

    private double GetMultiply()
    {
        return _one * _two;
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

public class Collection :  ObservableCollection<Numbers>  
{
    public ObservableCollection<Numbers> Numbers { get; set; }

    public Collection()
    {
        Numbers = new ObservableCollection<Numbers>();

        Numbers.Add(new Numbers(1, 2));
        Numbers.Add(new Numbers(2, 2)); 
    }
}

XAML:

<DataGrid x:Name="StockData" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" ItemsSource="{Binding}" AutoGenerateColumns="False" LostFocus="StockData_LostFocus" >
    <DataGrid.Columns >
        <DataGridTextColumn Header="Number One" Width="100" Binding="{Binding One, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat=\{0:C\}}" />
        <DataGridTextColumn Header="Number Two" Width="100" Binding="{Binding Two, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, StringFormat=\{0:C\}}" />
        <DataGridTextColumn Header="Total" Width="100" Binding="{Binding Multiply, BindsDirectlyToSource=True, Mode=TwoWay, StringFormat=\{0:C\}, UpdateSourceTrigger=PropertyChanged}" />
    </DataGrid.Columns>
</DataGrid>

Upvotes: 0

Views: 1126

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81243

You are setting multiply after raising PropertyChanged event, so UI won't get notification that value has been changed.

Set Multiply and then raise PropertyChanged event:

private void UpdateValue()
{
    _multiply = GetMultiply();
    OnPropertyChanged("One");
    OnPropertyChanged("Two");
    OnPropertyChanged("Multiply");
}

Upvotes: 2

Related Questions