Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8115641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:32:09+00:00 2026-06-06T03:32:09+00:00

I am trying to add my data into multiple columns ListBox, I did it

  • 0

I am trying to add my data into multiple columns ListBox, I did it but I am facing a hard problem when trying to retrieve the Data from the list box. is there a way to put an object instead of text into a listBox row?

<ListView Name="listBox1" ItemsSource="{Binding Items}" Margin="28,28,68,67" FlowDirection="RightToLeft" MouseDoubleClick="listBox1_MouseDoubleClick">
        <ListView Name="listBox1" ItemsSource="{Binding Items}" Margin="28,28,68,67" FlowDirection="RightToLeft" MouseDoubleClick="listBox1_MouseDoubleClick">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn Header="a" Width="100" DisplayMemberBinding="{Binding Path=ID}" />
                    <GridViewColumn Header="b" Width="100" DisplayMemberBinding="{Binding Path=Name}" />
                    <GridViewColumn Header="c" Width="100" DisplayMemberBinding="{Binding Path=F}" />
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

and this is the code

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    public sealed class MyListBoxItem
    {
        public string Field1 { get; set; }
        public string Field2 { get; set; }
        public string Field3 { get; set; }
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        Students st = new Students(1, "name","anything");
        listBox1.ItemsSource = new List(new[] { st });
    }

    private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        object ob = listBox1.SelectedItem;
        string i = ((MyListBoxItem)listBox1.SelectedItem).Field1;
    }
}

and here is the class Students

 class Students
{
    int id;
    string name;
    string f;

    public Students(int id, string name,string f)
    {
        this.id = id;
        this.name = name;
        this.f = f;
    }
    public int ID
    {
        get { return id; }
        set { id = value; }
    }
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    public string F
    {
        get { return f; }
        set { f = value; }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:32:10+00:00Added an answer on June 6, 2026 at 3:32 am

    Don’t use listBox1.Items.Add(….). Rather use listBox1.ItemsSource = new List(new[] {st});

    Then change Your DisplayMemberBindings to “Id”, “Name” respectively.

    There is no need for the ListBoxItem Class.

    == EDIT ==

    You were very close to getting it perfectly. I’ve attached below how it should work. The important things to notice are the Bindings in the ListView for ItemsSource and SelctedITem, and setting IsSynchronisedWithCurrentItem to true.

    Also, in the bottom two rows ofthe grid, I’ve shown two different ways of binding to the selected item, one using “/” notation, and the other using a property on the ViewModel

    XAML

    <Window x:Class="StackOverflow11087468.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
                <ListView Name="listBox1"
                          Grid.Row="0"
                          ItemsSource="{Binding Students}"
                          SelectedItem="{Binding SelectedStudent}"
                          IsSynchronizedWithCurrentItem="True"
                          Margin="28,28,68,67"
                          FlowDirection="RightToLeft">
                    <ListView.View>
                        <GridView>
                            <GridView.Columns>
                                <GridViewColumn Header="a"
                                                Width="100"
                                                DisplayMemberBinding="{Binding Path=ID}" />
                                <GridViewColumn Header="b"
                                                Width="100"
                                                DisplayMemberBinding="{Binding Path=Name}" />
                                <GridViewColumn Header="c"
                                                Width="100"
                                                DisplayMemberBinding="{Binding Path=F}" />
                            </GridView.Columns>
                        </GridView>
                    </ListView.View>
                </ListView>
    
            <StackPanel Grid.Row="1" Orientation="Horizontal">
                <TextBlock>ID</TextBlock>
                <TextBox Text="{Binding Students/ID}" />            
            </StackPanel>
    
            <StackPanel Grid.Row="2"
                        Orientation="Horizontal">
                <TextBlock>ID</TextBlock>
                <TextBox Text="{Binding SelectedStudent.ID}" />
            </StackPanel>
        </Grid>
    </Window>
    

    Main.Window.cs

    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 StackOverflow11087468
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                this.DataContext = new ViewModel();
            }
        }
    }
    

    ViewModel.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    
    namespace StackOverflow11087468
    {
        public class ViewModel : INotifyPropertyChanged
        {
            public ObservableCollection<Student> Students { get; set; }
    
            public ViewModel()
            {
                this.Students = new ObservableCollection<Student>();
                Students.Add(new Student(98760987, "Student1", "F"));
                Students.Add(new Student(98760988, "Student22", "M"));
            }
    
            public Student SelectedStudent
            {
                get { return _selectedStudent; }
                set
                {
                    _selectedStudent = value;
                    RaisePropertyChanged("SelectedStudent");
                }
            }
    
            private void RaisePropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            private Student _selectedStudent;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add data into an attribute of a core data entity. Contents
I am using nhibernate and mvc3 in asp.net I'm trying to add data into
I have a list and I am trying to add the data to the
I'm trying to add multiple jQuery data entries to a single element. I suspected
Need to load data from a single file with a 100,000+ records into multiple
Need to load data from a single file with a 100,000+ records into multiple
I am trying to export data from my MySQL database to multiple CSV files
I'm trying to get some aggregate values from different tables, but my problem is
I'm trying to load a default set of data from a csv file into
I tried to retrieve multiple rows of JSON data and display it but im

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.