How do I get the cell content value of the first column of the selected row of listview in WPF VB.NET?

Answered How do I get the cell content value of the first column of the selected row of listview in WPF VB.NET?

  • Saturday, September 01, 2012 9:19 PM
     
      Has Code

    The XAML for my listview is given below:

    <ListView Grid.Row="2" Name="ListView1" 
                      IsSynchronizedWithCurrentItem="True"
                      ItemsSource="{Binding Path={}}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Path=ID}" Width="40"/>
                        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}" Width="150"/>
                    </GridView>
                </ListView.View>            
    </ListView>

    I want to get the value of the first column of the selected row when the row is double clicked. But how? I am using VS2010, VB. Please help me.


    -k@N@k-

All Replies

  • Sunday, September 02, 2012 7:00 AM
     
     Answered

    Hi,

    You can do it like this

    <Window x:Class="WpfApplicationListView.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Grid>
            <StackPanel>
                <ListView Grid.Row="2" Name="ListView1" SelectionChanged="ListView1_SelectionChanged" PreviewMouseDoubleClick="ListView1_PreviewMouseDoubleClick"
                      IsSynchronizedWithCurrentItem="True"
                      ItemsSource="{Binding Path={}}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Path=ID}" Width="40"/>
                            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}" Width="150"/>
                        </GridView>
                    </ListView.View>
                </ListView>
            </StackPanel>
        </Grid>
    </Window>



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    namespace WpfApplicationListView
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public List<Student> Students { get; set; }

            public Window1()
            {
                InitializeComponent();
                Students=new List<Student>();
                Students.Add(new Student(1, "A"));
                Students.Add(new Student (2 ,"B"));
                Students.Add(new Student(3, "C"));
                ListView1.DataContext = Students;
            }

            private void ListView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {

            }

            private void ListView1_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
            {
                if (ListView1.SelectedItem != null)
                {
                    Student stu = (Student)ListView1.SelectedItem;
                    int Id = stu.ID;
                }
            }


        }



        public class Student
        {
            public Student(int id, string name)
            {
                ID = id;
                Name = name;
            }

            public int ID { get; set; }
            public string Name { get; set; }
        }
    }