Hi, I am new in WPF, i need to bind textbox text to listbox items.
This is My XAML CODE: My XAML code have 3 columns.
- First column have one button, that button generated textbox automatically when user double click the mouse.
- Second column is like canvas display the textbox
- Third column is listbox.
When user click the button in stackpanel1, Textbox automatically generated into the Second_Stackpanel. Then, user type text into the textbox, that text automatically bind into listboxitem(stackpanel3).(user change the same text into the textbox, in that text also update the same listboxitem index.)
User click the button again another textbox automatically generated, that text bind to listboxitem in next index...
How to bind Textbox to ListboxItems in wpf please help me
I find on google and stackoverflow site (listbox to textbox bind only here). but i need to textbox to listbox binding.
<Window x:Class="WpfApp5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp5"
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
xmlns:Control="clr-namespace:Syncfusion.Windows.Tools.Controls;assembly=Syncfusion.Tools.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Background="LightCoral" x:Name="stackpanel1">
<Button Height="40" Width="60" Content="Text" Background="Black" Foreground="Red"
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20" FontSize="20"
MouseDoubleClick="TextBox_DynamicallyGenerated_Mouse_DoubleClick"></Button>
</StackPanel>
<StackPanel Grid.Column="1" Background="White" x:Name="stackpanel2">
</StackPanel>
<StackPanel Grid.Column="2" Background="Gray" x:Name="stackpanel3">
<ListBox Height="300" x:Name="lstboxitems" Background="Aquamarine" Margin="20"></ListBox>
</StackPanel>
</Grid>
</Window>
This is My Code-Behind
public partial class MainWindow : Window
{
public static TextBox textBox;
public MainWindow()
{
InitializeComponent();
}
private void TextBox_DynamicallyGenerated_Mouse_DoubleClick(object sender, MouseButtonEventArgs e)
{
textBox = new TextBox();
textBox.Text = "Enter Text Here";
textBox.Width = 120;
textBox.Height = 25;
textBox.Background = Brushes.White;
textBox.BorderBrush = Brushes.Red;
textBox.Name = "txtbox";
textBox.IsEnabled = false;
textBox.Foreground = Brushes.Red;
stackpanel2.Children.Add(textBox);
Canvas.SetLeft(textBox, e.GetPosition(stackpanel2).X);
Canvas.SetTop(textBox, e.GetPosition(stackpanel2).Y);
textBox.MouseDoubleClick += TextBox_MouseDoubleClick;
}
private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
textBox = sender as TextBox;
textBox.IsEnabled = true;
textBox.MouseLeave += TextBox_MouseLeave;
}
private void TextBox_MouseLeave(object sender, MouseEventArgs e)
{
textBox = sender as TextBox;
textBox.IsEnabled = false;
}
}