2009-12-25 79 views
2

XAML:如何从代码隐藏中访问ListBox动态创建项目的属性?

<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? 

    } 
} 
} 

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

(在上面的图片,我想当我点击“获取值”按钮时,获得值“b”。)

我想知道是否有简单实现这一目标的方式。

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

回答

2

做的“WPF道路”你想要什么是使用数据绑定:

  1. 定义一个类叫做文本字符串属性。
  2. 创建该类的集合。
  3. 将您的列表框ItemsSource绑定到集合。
  4. 创建一个DataTemplate,它使用{Binding Path = Text}绑定其文本属性来显示文本框。
  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> 
+0

非常感谢您的答案! 但实际上我想得到的不是关于文本框,它只是一个示例。我只想访问像ListBox这样的容器内的项目的属性。 如果有人正在寻找相同的东西,那么你可以试试这个: var tb = lb.Items [0] as TextBox; 在那里,只要这样,那么你可以访问任何你想要的物品。 – 2009-12-28 22:59:10

0

否无针对TextBox:s。 ListBox处理字符串很好。

​​
+0

@mG:如果没有TextBox功能的实际需求,但只有一个字符串容器,请遵循Tuukka的解决方案。 – Ucodia 2009-12-25 22:43:37

0

如果你只是想获得所选文本框的Text属性(admiting你的列表框处于单选择模式),它也很简单:

private void btnGet_Click(object sender, RoutedEventArgs e) 
{ 
    if(lb.SelectedItem != -1) 
    { 
     TextBox selectedTextBox = (TextBox)lb.SelectedItem; 
     txtReturn.Text = selectedTextBox.Text; 
    } 
} 

但是,如果你要实现的漂亮WPF的方式,你应该遵循Aviad P.解决方案,我的解决方案也做得很好。

问候。


编辑:如果没有文本框的功能性的实际需要,但只有一个字符串的容器,所以按照Tuukka的解决方案。

1

的复选框项:

private void chk_Checked(object sender, RoutedEventArgs e) 
{ 
    CheckBox chk = (CheckBox)sender; 
    MessageBox.Show(chk.Content.ToString()); 
} 
相关问题