gpt4 book ai didi

wpf - 如何从代码隐藏访问 ListBox 动态创建项的属性?

转载 作者:行者123 更新时间:2023-12-04 14:43:16 25 4
gpt4 key购买 nike

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>

夏普:

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?

}
}
}

和截图:(对不起,我不允许直接发布图片) http://i825.photobucket.com/albums/zz180/mGlushed/get_listbox_item_property.png

(在上图中,我想在单击“获取值”按钮时获取值“b”。)

我想知道是否有一种简单的方法可以实现这一点。

我是 WPF 的新手,所以我只知道从头开始做这件事,即:创建一个数组。每次创建新的 TextBox 时,将其添加到数组中。然后通过数组访问文本框。但我认为这听起来不是很理想。

最佳答案

做你想做的事情的“WPF 方式”是使用数据绑定(bind):

  1. 使用名为 Text 的字符串属性定义一个类。
  2. 创建该类的集合。
  3. 将您的列表框 ItemsSource 绑定(bind)到集合。
  4. 创建一个显示 TextBox 的 DataTemplate,其 Text 属性使用 {Binding Path=Text} 绑定(bind)。
  5. 在 btnAdd_Click 中添加一个项目到集合中(不是直接添加到 ListBox 中)
  6. 在 btnGet_Click 中,您可以通过将 ListBox.SelectedItem 转换为您的类并获取其 Text 属性来获取输入的文本。

例子:

简单类:

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

public string Text { get; set; }
}

窗口代码隐藏:

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;
}
}

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>

关于wpf - 如何从代码隐藏访问 ListBox 动态创建项的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1961875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com