How to update the value of a cell based on another in the Grid View WPF

3

I have the following Grid, I would like to know how can I do so that when I leave the Gain or Spend field it updates the Total?

If there is another way to do it, I'm happy to meet you.

Total = (Gain - Expense)

    private void txtGanho_LostFocus(object sender, RoutedEventArgs e)
    {
        TextBox txtGanho = (TextBox)sender;
        //Aqui eu tenho o valor do Ganho, como posso encontrar na Grid os TextBox com Gasto e Total
    }

    private void txtGasto_LostFocus(object sender, RoutedEventArgs e)
    {
        TextBox txtGasto = (TextBox)sender;
        //Aqui tenho o valor do Gasto, como posso encontrar na Grid os TextBox com Ganho e Total            
    }     

.xaml file

   <DataGrid x:Name="dgControleFinanceiro" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Id" Binding="{Binding Id}" IsReadOnly="True" />
            <DataGridTextColumn Header="Data" Binding="{Binding Data}" IsReadOnly="True"/>

            <DataGridTemplateColumn Header="Ganho do dia"  IsReadOnly="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Name="txtGanho" Text="{Binding Ganho}" LostFocus="txtGanho_LostFocus"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn Header="Gasto do dia"  IsReadOnly="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Name="txtGasto" Text="{Binding Gasto}" LostFocus="txtGasto_LostFocus"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn Header="Total do dia" IsReadOnly="true">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Name="txtTotal" Text="{Binding Total}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>
    </DataGrid>
    
asked by anonymous 09.11.2015 / 18:46

1 answer

1

The best way to do this is through ViewModel . This ViewModel should implement the INotifyPropertyChanged interface. % Binding% of properties UpdateSourceTrigger and ValorGanho must be ValorGasto , that when it loses focus, LostFocus will be executed. So whenever a set occurs in set or ValorPago , View will be notified of the change in ValorGanho , reflecting on the grid.

public class ControleFinanceiroViewModel : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged = delegate { };

   protected void NotificarMudancaPropriedade([CallerMemberName] string nomePropriedade = null)
   {
     if (nomePropriedade != null)
     {
        PropertyChanged(this, new PropertyChangedEventArgs(nomePropriedade));
     }
   }

   private decimal valorGasto;
   public decimal ValorGasto
   {
     get { return this.valorGasto; }
     set
     {
       this.valorGasto = value;
       this.NotificarMudancaPropriedade();
       this.NotificarMudancaPropridade("Total");
     }
   }

   private decimal valorGanho;
   public decimal ValorGanho
   {
     get { return this.valorGanho; }
     set
     {
       this.valorGanho= value;
       this.NotificarMudancaPropriedade();
       this.NotificarMudancaPropridade("Total");
     }
   }

   public decimal Total 
   {
     get { return this.ValorGanho - this.ValorGasto; }
   }
}

The Binding will look like this, and you remove the event Total

<DataTemplate>
    <TextBox Name="txtGanho" Text="{Binding Ganho, Mode = TwoWay, UpdateSourceTrigger = LostFocus}" />
</DataTemplate>

It is not good practice in WPF to work with events since it can work with MVVM. And retrieving objects from another grid cell in WPF can be very laborious. So I leave this approach.

link

    
20.11.2015 / 13:14