1

Hi all. When ListView item is double clicked it should display the corresponding data to text box, later i will write some code to update.

<p>Below is listview code on my xaml file</p>  
  <ListView Margin="534,233,10,18" Name="lvCus"  MouseDoubleClick="ListViewItem_DoubleClick">
        <ListView.View>
            <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="TbCus">
                <GridViewColumn Header="CusID" Width="40" DisplayMemberBinding="{Binding Path=CusID}" />
                <GridViewColumn Header="fn" Width="120" DisplayMemberBinding="{Binding Path=fn}" />
                <GridViewColumn Header="Lastname" Width="120" DisplayMemberBinding="{Binding Path=ln}" />
                <GridViewColumn Header="Dob" Width="100" DisplayMemberBinding="{Binding Path=dob}" />
                <GridViewColumn Header="Age" Width="60" DisplayMemberBinding="{Binding Path=age}" />
            </GridView>
        </ListView.View>
    </ListView>

Below is code at backend.

    private void ListViewItem_DoubleClick(object sender, RoutedEventArgs e)
    {
            ListViewItem item = lvCus.SelectedItems[0];
            txtfn.Text = item.SubItems[1].Text;
            txtln.Text = item.SubItems[2].Text;
            txtdob.Text = item.SubItems[3].Text;
    }

I see red underline near the code lvCus.SelectedItems[0] , Error: cannot implicitly convert type 'object' to 'System.Windows.Controls.ListViewItem'. An explict conversion exists (are you missing a cast?)

Any help would be appreciated. Thanks.

Il Vic
  • 5,576
  • 4
  • 26
  • 37
siv rj
  • 1,451
  • 1
  • 14
  • 31

1 Answers1

2

You need to cast to your type of object say Customer

Customer custObj = (Customer)lvCus.SelectedItems[0];

Then you can access the properties

txtfn.Text = custObj.fn;
txtln.Text = custObj.ln;
txtdob.Text = custObj.dob;
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396