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 512877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:21:53+00:00 2026-05-13T07:21:53+00:00

XAML: <Window x:Class=WpfApp_ListBoxTest.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> <ListBox Name=lb Margin=0,0,0,70></ListBox> <Button Height=23

  • 0

XAML:

<Window x:Class="WpfApp_ListBoxTest.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>
  <ListBox Name="lb" Margin="0,0,0,70"></ListBox>
  <Button Height="23" HorizontalAlignment="Left" Margin="12,0,0,41" Name="btnAdd" VerticalAlignment="Bottom" Content="Add item" Width="75" Click="btnAdd_Click"></Button>
  <TextBox Height="23" Margin="93,0,12,41" Name="txtInput" VerticalAlignment="Bottom" />
  <Button Height="23" HorizontalAlignment="Left" Margin="12,0,0,12" Name="btnGet" VerticalAlignment="Bottom" Content="Get value" Width="75" Click="btnGet_Click"></Button>
  <TextBox Height="23" Margin="93,0,12,12" Name="txtReturn" VerticalAlignment="Bottom" IsReadOnly="True" />
 </Grid>
</Window>

Csharp:

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;
using System.Xml;

namespace WpfApp_ListBoxTest
{
 /// <summary>
 /// Interaction logic for Window1.xaml
 /// </summary>
 public partial class Window1 : Window
 {
  public Window1()
  {
   InitializeComponent();
  }

  private void btnAdd_Click(object sender, RoutedEventArgs e)
  {
   TextBox txt = new TextBox();
   txt.Width = 200;
   txt.Text = txtInput.Text;
   lb.Items.Add(txt);
  }

  private void btnGet_Click(object sender, RoutedEventArgs e)
  {
   // What do I need to write here to get the value of the Text property of the selected TextBox?

  }
 }
}

And screenshot: (Sorry I’m not allowed to post picture directly)
http://i825.photobucket.com/albums/zz180/mGlushed/get_listbox_item_property.png

(In the picture above, I want to get the value “b” when I click the “Get value” button.)

I would like to know if there is a simple way to achieve this.

I’m new to WPF, so I only know to do this the long way, which is: Create an array. Everytime a new TextBox is created, add it into the array. Then access the TextBox’es through the array. But that doesn’t sound very optimal, I think.

  • 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-05-13T07:21:53+00:00Added an answer on May 13, 2026 at 7:21 am

    The ‘WPF Way’ of doing what you want is to use data binding:

    1. Define a class with a string property called Text.
    2. Create a collection of that class.
    3. Bind your list box ItemsSource to the collection.
    4. Create a DataTemplate that shows a TextBox with its Text property bound using {Binding Path=Text}.
    5. In btnAdd_Click add an item to the collection (not directly to the ListBox)
    6. In btnGet_Click you can get the text entered by casting ListBox.SelectedItem to your class and getting its Text property.

    Example:

    The simple class:

    public class VMObject
    {
        public VMObject(string text)
        {
            Text = text;
        }
    
        public string Text { get; set; }
    }
    

    The window code-behind:

    public partial class Window1 : Window
    {
        public ObservableCollection<VMObject> VM { get; set; }
    
        public Window1()
        {
            VM = new ObservableCollection<VMObject>();
            InitializeComponent();
        }
    
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            VM.Add(new VMObject(txtInput.Text));
        }
    
        private void btnGet_Click(object sender, RoutedEventArgs e)
        {
            if (lb.SelectedItem == null)
                MessageBox.Show("No item is selected!");
            txtReturn.Text = ((VMObject)lb.SelectedItem).Text;
        }
    }
    

    The XAML:

    <Window x:Class="lbtest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Name="Window"
        Title="Window1" Height="300" Width="300">
        <Window.Resources>
            <DataTemplate x:Key="TextBoxTemplate">
                <TextBox Text="{Binding Path=Text}"/>
            </DataTemplate>
        </Window.Resources>
        <Grid>
            <ListBox Name="lb" Margin="0,0,0,70"
                     ItemsSource="{Binding ElementName=Window, Path=VM}"
                     ItemTemplate="{StaticResource TextBoxTemplate}" />
            <Button Height="23" HorizontalAlignment="Left" Margin="12,0,0,41"
                    Name="btnAdd" VerticalAlignment="Bottom"
                    Content="Add item" Width="75" Click="btnAdd_Click" />
            <TextBox Height="23" Margin="93,0,12,41"
                     Name="txtInput" VerticalAlignment="Bottom" />
            <Button Height="23" HorizontalAlignment="Left" Margin="12,0,0,12"
                    Name="btnGet" VerticalAlignment="Bottom"
                    Content="Get value" Width="75" Click="btnGet_Click" />
            <TextBox Height="23" Margin="93,0,12,12"
                     Name="txtReturn" VerticalAlignment="Bottom" IsReadOnly="True" />
        </Grid>
    </Window>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
If I have a Xaml Window, how does one open it as a child
Why i cant set selecteditem property programatically? Im calling it from another XAML Window,
Suppose I have some XAML like this: <Window.Resources> <v:MyClass x:Key=whatever Text=foo\nbar /> </Window.Resources> Obviously
OK I'm redoing this since I messed it up. :) here's the xaml. <Window
XAML allows you to specify an attribute value using a string that contains curly
XAML: <ToolBarTray Name=tlbTray ButtonBase.Click=tlbTray_Click> <ToolBar Name=tlbFile> <Button Name=btnOpen><Image Source=images\folder.png Stretch=None /></Button> <Button Name=btnSave><Image Source=images\disk.png
Since XAML gets compiled, there should be no difference in execution of code like
When editing XAML in VS2008 SP1, the editor is really slow. devenv process seems
I have the following XAML: <TextBlock Text={Binding ElementName=EditListBox, Path=SelectedItems.Count} Margin=0,0,5,0/> <TextBlock Text=items selected> <TextBlock.Style>

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.