Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello people; I’m new to development, and I’m training creating small software.
my idea is to be able to take a date from the datagrid, the model and pass it to a ViewModel property. but I don't know how to start can someone help me?

What I have tried:

in view:

XML
<DataGrid Padding ="5" VerticalScrollBarVisibility="Visible"
                      HorizontalScrollBarVisibility="Visible" FontSize="14"
                      Name="dgLista" IsReadOnly="True" ItemsSource="{Binding Path=Consultas}" SelectionMode="Single">
                    <DataGrid.Columns>
                        <DataGridTemplateColumn Header="Detalhe">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Name="btnDetalhe" Content="Detalhe" CommandParameter="{Binding}"
                                        Command="{Binding Path= DetalheCommand , Mode=TwoWay}" Click="btnDetalhe_Click"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    </DataGrid.Columns>
                    
                </DataGrid>


in viewModel:

C#
private DateTime dataProcura;

        public DateTime DataProcura {
            get { return dataProcura; }
            set { SetField(ref dataProcura, value); }
        }

       public CommandBase DetalheCommand { get; set;  }

 public AutomovelViewModel()
        {
            
            DataInicial = DateTime.UtcNow; 
            dataFinal = DateTime.Now;
            
            try
            {
      
                DetalheCommand = new CommandBase(ControleDetalhe); 

            }catch(Exception e)
            {
                System.Windows.MessageBox.Show("Ocorreu um erro: " + e.Message);
            }

        private void ControleDetalhe()
        {
            
            //Pegar a data na linha selecionada e passar para dentro de uma variavel. 
            //int Index = dgLista.CurrentCell.RowIndex;


            dao = new AutomovelBuscaDao();
            dao.ControleDetalhes(DataProcura); 
        }
Posted
Updated 1-Jun-20 2:26am
v2

1 solution

Using an ObservableCollection is recommended, see example here: c# - Datagrid binding in WPF - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900