WPF 文本框到 ListBoxItem 绑定
WPF Textbox to ListBoxItem Binding
Hi, I am new in WPF, i need to bind textbox text to listbox items.
这是我的 XAML 代码:
我的 XAML 代码有 3 列。
- 第一列有一个按钮,当用户双击鼠标时,该按钮自动生成文本框。
- 第二列类似canvas显示文本框
- 第三列是列表框。
当用户点击stackpanel1中的按钮时,Textbox自动生成进入Second_Stackpanel。然后,用户在文本框中键入文本,该文本自动绑定到列表框项(stackpanel3)。(用户将相同的文本更改为文本框,在该文本中也更新相同的列表项索引。)
用户再次单击该按钮会自动生成另一个文本框,该文本绑定到下一个索引中的列表框项...
如何在 wpf 中将 Textbox 绑定到 ListboxItems 请帮助我
我在 google 和 Whosebug 网站上找到(列表框到文本框的绑定只在这里)。但我需要将文本框绑定到列表框。
<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;
}
}
您可以在创建文本框时在代码后面设置绑定。
TextBlock lisboxItemTxtBlock = new TextBlock();
this.lstboxitems.Items.Add(new ListBoxItem { Content = lisboxItemTxtBlock });
Binding binding = new Binding("Text");
binding.Source = textBox;
BindingOperations.SetBinding(lisboxItemTxtBlock, TextBlock.TextProperty, binding);
我有几点可能会有所帮助:
在您发布的代码中,当您可能想要生成一个唯一的名称并跟踪它以便您可以稍后进行其他修改时,您将文本框的名称设置为“txtbox”。
您将文本框的 IsEnabled 属性 设置为 false,并在 MouseDoubleClick 上添加了一个事件。这不会起作用,事件不会在未启用的文本框上触发。你可以看看:Disable a WPF button, but still swallow the click events
虽然这是我找到的回答问题的最佳方式,但请注意,这样做对 MVVM 不友好,因此您可能需要在做出决定之前检查 WPF 的 MVVM 模式.
Hi, I am new in WPF, i need to bind textbox text to listbox items.
这是我的 XAML 代码: 我的 XAML 代码有 3 列。
- 第一列有一个按钮,当用户双击鼠标时,该按钮自动生成文本框。
- 第二列类似canvas显示文本框
- 第三列是列表框。
当用户点击stackpanel1中的按钮时,Textbox自动生成进入Second_Stackpanel。然后,用户在文本框中键入文本,该文本自动绑定到列表框项(stackpanel3)。(用户将相同的文本更改为文本框,在该文本中也更新相同的列表项索引。)
用户再次单击该按钮会自动生成另一个文本框,该文本绑定到下一个索引中的列表框项...
如何在 wpf 中将 Textbox 绑定到 ListboxItems 请帮助我
我在 google 和 Whosebug 网站上找到(列表框到文本框的绑定只在这里)。但我需要将文本框绑定到列表框。
<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;
}
}
您可以在创建文本框时在代码后面设置绑定。
TextBlock lisboxItemTxtBlock = new TextBlock();
this.lstboxitems.Items.Add(new ListBoxItem { Content = lisboxItemTxtBlock });
Binding binding = new Binding("Text");
binding.Source = textBox;
BindingOperations.SetBinding(lisboxItemTxtBlock, TextBlock.TextProperty, binding);
我有几点可能会有所帮助:
在您发布的代码中,当您可能想要生成一个唯一的名称并跟踪它以便您可以稍后进行其他修改时,您将文本框的名称设置为“txtbox”。
您将文本框的 IsEnabled 属性 设置为 false,并在 MouseDoubleClick 上添加了一个事件。这不会起作用,事件不会在未启用的文本框上触发。你可以看看:Disable a WPF button, but still swallow the click events
虽然这是我找到的回答问题的最佳方式,但请注意,这样做对 MVVM 不友好,因此您可能需要在做出决定之前检查 WPF 的 MVVM 模式.