我创建了一个带TextBox Control的ListView。我需要在TextBox中获取值。
用户在Textbox上输入后。我需要输入用户输入的内容。
<Window x:Class="LdiaryEditableListView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Ldiary Editable ListView Sample" Height="350" Width="300">
<StackPanel >
<ListView Name="listView">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<EventSetter Event="Button.Click" Handler="Button_Click"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=FirstName}" Value="">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Width="100" Text="{Binding Path=FirstName}"/>
<TextBox Width="100" Text="{Binding Path=LastName}"/>
<Button Width="70" >Add</Button>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="First Name" DisplayMemberBinding="{Binding Path=FirstName}"></GridViewColumn>
<GridViewColumn Width="100" Header="Last Name" DisplayMemberBinding="{Binding Path=LastName}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
Iam创建了一个函数来获取listview中文本框中的每个元素。但它不能正常工作
foreach(DataRowView itm in lstvQualification.Items)
{
MessageBox.Show(itm[0].ToString());
}
答案 0 :(得分:0)
我解决了我的问题。
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace LdiaryEditableListView
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ObservableCollection<Person> people;
public MainWindow()
{
InitializeComponent();
people = new ObservableCollection<Person>(){
new Person{FirstName="", LastName=""},
new Person{FirstName = "Ldiary", LastName="Translations"},
new Person{FirstName = "English", LastName="Japanese"}
};
listView.ItemsSource = people;
}
public class Person : INotifyPropertyChanged
{
private string _firstName;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
private string _lastname;
public string LastName
{
get
{
return _lastname;
}
set
{
_lastname = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Person newPerson = people[0];
if (string.IsNullOrEmpty(newPerson.FirstName) || string.IsNullOrEmpty(newPerson.LastName))
{
newPerson.FirstName = "";
newPerson.LastName = "";
MessageBox.Show("Please provide both first name and last name!");
}
else
{
Person emptyPerson = new Person()
{
FirstName = "",
LastName = ""
};
people.Insert(0, emptyPerson);
}
listView.ItemsSource = null;
listView.ItemsSource = people;
}
}
}