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

  • SEARCH
  • Home
  • 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 4028880
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:16:32+00:00 2026-05-20T11:16:32+00:00

I want to create a 2 way bind between a listbox and a .NET

  • 0

I want to create a 2 way bind between a listbox and a .NET list.

In my GUI, I have a listbox, a textbox and add and remove buttons.
The listbox displays cars, and my goal is to create a two-way bind between the .Net car list and the listbox: when the user enters a car into the textbox, it gets updated only in the .Net list, and the listbox is updated automatically.

When the user press the GUI “remove” button, a car gets removed from the GUI and the .Net list is updated automatically.

I’ve started to write the xaml code, but figured that I don’t actually know how to do the binding on both sides (c# and xaml):

<Window x:Class="WpfApplication1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="369" Loaded="Window_Loaded">
    <Window.Resources>
        <ObjectDataProvider x:Key="carsData" 
                        ObjectType="{x:Type c:Window1}" />
    </Window.Resources>
    <Grid Width="332">
        <ListBox Margin="10,62,0,100" Name="myListBox" HorizontalAlignment="Left" Width="120" ItemsSource="{Binding Source={StaticResource CarsData}}"/>
        <Button Height="23" Margin="66,0,0,65" Name="addBtn" VerticalAlignment="Bottom" Click="addBtn_Click" HorizontalAlignment="Left" Width="64">add</Button>
        <TextBox Margin="10,0,0,64.48" Name="myTextBox" Height="23" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="47" />
        <Button Height="23" Margin="66,0,0,33" Name="removeButton" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="64" Click="removeButton_Click">Remove</Button>
    </Grid>
</Window>

There is my c# code:

public partial class Window1 : Window
{
    MyModel listMgr;
    ObservableCollection<Car> carList;

    public Window1()
    {
        InitializeComponent();
        listMgr = new MyModel();
    }

    private void addBtn_Click(object sender, RoutedEventArgs e)
    {
        listMgr.add(new Car(0, myTextBox.Text, 2011));

    }

    private void removeButton_Click(object sender, RoutedEventArgs e)
    {
        //myListBox.Items.RemoveAt(0);
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        carList = listMgr.getList();
        myListBox.DataContext = carList;
        //secondListBox.DataContext = carList;
    }
}
  • 1 1 Answer
  • 1 View
  • 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-05-20T11:16:32+00:00Added an answer on May 20, 2026 at 11:16 am

    This is a quick version, you’ll need to add code to check the car is selected, etc.

    To see your Car data you’ll need to define a data template. In the example it’s just a simple name, but you can change the text colour, font size, add more fields, etc.

    It’s best to work on the list when adding / removing cars, not on the ListBox directly.

    XAML:

    <Window x:Class="cars.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">
    <Window.Resources>
        <DataTemplate x:Key="car_template" DataType="Car">
            <TextBlock Text="{Binding name}"/>
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
        <ListBox x:Name="cars_box" Margin="5" ItemsSource="{Binding}" ItemTemplate="{StaticResource car_template}"/>
        <TextBox x:Name="new_car_box" Margin="5"/>
        <Button Content="add" Click="add_car" Margin="5"/>
        <Button Content="delete" Click="delete_car" Margin="5"/>
    </StackPanel>
    

    C#:

    using System.Collections.ObjectModel;
    using System.Windows;    
    public partial class MainWindow : Window
    {
        ObservableCollection<Car> cars = new ObservableCollection<Car>();
        public MainWindow()
        {
            InitializeComponent();            
            cars.Add(new Car("Volvo"));
            cars.Add(new Car("Ferrari"));
            cars_box.DataContext = cars;
        }
    
        private void add_car(object sender, RoutedEventArgs e)
        {
            cars.Add(new Car(new_car_box.Text));
        }
    
        private void delete_car(object sender, RoutedEventArgs e)
        {
            cars.Remove((cars_box.SelectedItem as Car));
        }
    }
    
    public class Car
    {
        public string name { get; set; }
        public Car(string _name)
        {
            this.name = _name;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to dynamically create a selectItem list? I dont really want
I want to create a layout in such a way that on top edittext
I want to create tabpage as userControl. Is there any way to deal with
I'm looking for as simple way to create an identity set. I just want
Im using VS 2005 and I want to find the best way to create
i want create image animation , i have 50 images with png format now
I want to create a table representing data I have, but I want each
I have a UITableViewCell and want to bind a custom object to it. The
If I have RabbitMQ installed on my machine, is there a way to create
I have a collection ObservableCollection<Foo> , and I want to bind this to a

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.