locked
Get SelectedItem from ListView RRS feed

  • Question

  • HI! I Have a problem with ListView. I can't get value which I selected. Before I add Item as class to ListView and after i can't get one selectedItem to do something with it. I show my code and please.. I'm beginner :)

    Problem is in Select_dir().. Can You help me? :(

     public MainWindow()
            {
                InitializeComponent();
                var drives = DriveInfo.GetDrives();
                foreach (DriveInfo dir in drives)
                {
                    Disc_1.Items.Add(dir.Name);
                    Disc_2.Items.Add(dir.Name);
                }
    
              
    
            }
    
            public class Files
            {
                public string name { get; set; }
                public string type { get; set; }
                public long size { get; set; }
    
                public DateTime date { get; set; }
    
                
            }
    
            public List<Files> myFile { get; set; }
    
            private void select_path(object sender, SelectionChangedEventArgs e)
            {
                string path;
                
    
                if ( Disc_1.SelectedIndex > -1)
                {
                    path = Disc_1.SelectedItem.ToString();
                    show_elements(path, Disc_1, lv_1);
                }
    
                if (Disc_2.SelectedIndex > -1)
                {
                    path = Disc_2.SelectedItem.ToString();
                    show_elements(path, Disc_2, lv_2);
                }
    
            }
    
            private void Select_dir(object sender, SelectionChangedEventArgs e)
            {
                if(lv_1.SelectedIndex > -1)
                {
    
                    Files file = new Files();
    
                    file = lv_1.SelectedItem;
    
                    string n = file.name;
    
    
                    
                }
            }
    
           
    
            private void Show_elements(string d, ComboBox disc , ListView lv )
            {
                
                lv.Items.Clear();
    
                string[] dirs = Directory.GetDirectories(d);
                string[] files = Directory.GetFiles(System.IO.Path.GetFullPath(d));
    
                foreach (string dir in dirs)
                {
                    disc.Items.Add(dir);
                    Files dire = new Files();
    
                    var dir_ = new DirectoryInfo(dir).Name;
                    var date_last = Directory.GetLastWriteTime(dir);
    
                    dire.name = dir_;
                    dire.type = "dir";
    
                    dire.date = date_last;
                    lv.Items.Add(dire);
    
                }
    
                myFile = new List<Files>();
    
    
                foreach (string f in files)
                {
                    Files file = new Files();
    
                    file.name = System.IO.Path.GetFileNameWithoutExtension(f);
                    file.type = System.IO.Path.GetExtension(f);
                    file.size = new FileInfo(f).Length;
                    file.date = new FileInfo(f).LastWriteTime;
    
                    lv.Items.Add(file);
    
    
    
    
                }
    
            }
    
            
        
    
        }
     

    <Grid>
            <ListView  SelectionChanged="Select_dir" Name="lv_1" ItemsSource="{Binding myFile}" HorizontalAlignment="Left" Height="218" Margin="21,50,0,0" VerticalAlignment="Top" Width="394">
                <ListView.View>
                    <GridView>
                        <GridViewColumn DisplayMemberBinding="{Binding name}" Header="Name" Width="140"/>
                        <GridViewColumn DisplayMemberBinding="{Binding type}" Header="Typ" Width="50"/>
                        <GridViewColumn DisplayMemberBinding="{Binding size}" Header="Size" Width="60"/>
                        <GridViewColumn DisplayMemberBinding="{Binding date}" Header="Date" Width="140"/>
                    </GridView>
                </ListView.View>
    
            </ListView>
            <ListView  SelectionChanged="Select_dir"  Name="lv_2" HorizontalAlignment="Left" Height="218" Margin="500,50,0,0" VerticalAlignment="Top" Width="377" RenderTransformOrigin="0.725,0.523">
                <ListView.View>
                    <GridView>
                        <GridViewColumn DisplayMemberBinding="{Binding name}" Header="Name" Width="140"/>
                        <GridViewColumn DisplayMemberBinding="{Binding type}" Header="Typ" Width="50"/>
                        <GridViewColumn DisplayMemberBinding="{Binding size}" Header="Size" Width="60"/>
                        <GridViewColumn DisplayMemberBinding="{Binding date}" Header="Date" Width="140"/>
    
                    </GridView>
                </ListView.View>
            </ListView>
            <Button Content="Open"  HorizontalAlignment="Left" Margin="33,292,0,0" VerticalAlignment="Top" Width="75"/>
            <Button Content="Copy" HorizontalAlignment="Left" Margin="248,292,0,0" VerticalAlignment="Top" Width="75"/>
            <Button Content="Move" HorizontalAlignment="Left" Margin="584,292,0,0" VerticalAlignment="Top" Width="75"/>
            <Button Content="Delete" HorizontalAlignment="Left" Margin="788,292,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="4.79,0.162"/>
            <ComboBox Name="Disc_1" SelectionChanged="select_path" HorizontalAlignment="Left" Margin="33,10,0,0" VerticalAlignment="Top" Width="184"/>
            <ComboBox Name="Disc_2" SelectionChanged="select_path" HorizontalAlignment="Left" Margin="679,10,0,0" VerticalAlignment="Top" Width="184" RenderTransformOrigin="1.605,0.713"/>
        </Grid>

    Thursday, June 7, 2018 7:41 PM

All replies

  • Hi BaNaw,

    This forum discuss and ask questions about the C# programming language, since your issue is more related to WPF, I will move it WPF forum for suitable support.

    Thanks for your understanding.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, June 8, 2018 6:09 AM
  • Hi BaNaw,

    Please could use as operator to convert to Files object.

     private void Select_dir(object sender, SelectionChangedEventArgs e)
    {
        if (lv_1.SelectedIndex > -1)
        {
             Files file = new Files();
             file = lv_1.SelectedItem as Files;
             string n = file.name;
        }
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, June 8, 2018 6:24 AM