Home > front end >  How to display selected item from ListView in DataGrid
How to display selected item from ListView in DataGrid

Time:08-02

I recently started learning WPF and c#. Sorry if the question is stupid.

I have some Class Client. Which inside contains the Class Account.

Information about the Client class In addition to the Account class, it is displayed in the ListView. Everything is good here.

There are also simple textboxes that link to the selected ListView item via

Text="{Binding ElementName=LVclientBase, Path=SelectedItem.phoneNumber}". Everything is fine here too.

Now I need to create a DataGrid that will display information from the Account class for the element selected in the Listview.I made by analogy with textBox. But that doesn't work.

<DataGrid Grid.Column="4" Grid.Row="1">
        <DataGrid.Columns>
            <DataGridTextColumn Header="isDeposit" Binding="{Binding ElementName=LVclientBase, Path=SelectedItem.accounts.isDeposit}"/>
            <DataGridTextColumn Header="accountNumber" Binding="{Binding ElementName=LVclientBase, Path=SelectedItem.accounts.accountNumber}"/>
            <DataGridTextColumn Header="balance" Binding="{Binding ElementName=LVclientBase, Path=SelectedItem.accounts.balance}"/>
        </DataGrid.Columns>
    </DataGrid>

I think that there is a problem with the fact that there is a list and not a single value, but I don’t understand how to make it work

CodePudding user response:

you need to bind ItemsSource to accounts collection and fix column bindings accordingly:

<DataGrid Grid.Column="4" Grid.Row="1" 
          ItemsSource="{Binding ElementName=LVclientBase, Path=SelectedItem.accounts}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="isDeposit" Binding="{Binding Path=isDeposit}"/>
        <DataGridTextColumn Header="accountNumber" Binding="{Binding Path=accountNumber}"/>
        <DataGridTextColumn Header="balance" Binding="{Binding Path=balance}"/>
    </DataGrid.Columns>
</DataGrid>
  • Related