3
I have the following Grid, I would like to know how I can do so that when I leave the Gain or Spend field it updates the Total?
If there’s any other way I’m happy to meet.
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
}
File . xaml
<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>
Thanks for your reply @Murilo as soon as I get I will try to implement this way. I didn’t know there was such a MVVM, do you know any material for me to learn? Because I program everything with events.
– Mauricio Ferraz
I don’t have specific material. I learned from forums and tutorials (via google itself); but you may be able to retrieve your textbox through Treevisual https://msdn.microsoft.com/pt-br/library/system.windows.media.visualtreehelper(v=vs.110). aspx http://www.wpftutorial.net/logicalandvisualtree.html
– user26552