我有一个程序将数据从4个textBox绑定到ListView。
XAML代码:
<Window x:Class="TestBindingData.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="324" Width="592">
<Grid>
<ListView Height="209" HorizontalAlignment="Left" Margin="12,64,0,0" Name="listView1" VerticalAlignment="Top" Width="546" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding STT}" Header="STT" Width="50"/>
<GridViewColumn DisplayMemberBinding="{Binding HVT}" Header="Name" Width="200"/>
<GridViewColumn DisplayMemberBinding="{Binding Age}" Header="Age" Width="50"/>
<GridViewColumn DisplayMemberBinding="{Binding State}" Header="State" Width="200"/>
</GridView>
</ListView.View>
</ListView>
<TextBox Height="22" HorizontalAlignment="Left" Margin="99,25,0,0" Name="textBox1" VerticalAlignment="Top" Width="45" />
<TextBox Height="22" HorizontalAlignment="Left" Margin="150,25,0,0" Name="textBox2" VerticalAlignment="Top" Width="138" />
<TextBox Height="22" HorizontalAlignment="Left" Margin="294,25,0,0" Name="textBox3" VerticalAlignment="Top" Width="45" />
<TextBox Height="22" HorizontalAlignment="Left" Margin="345,25,0,0" Name="textBox4" VerticalAlignment="Top" Width="92" />
<Button Content="Add" Height="23" HorizontalAlignment="Left" Margin="461,27,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
MainWindow代码:
namespace TestBindingData
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
class person
{
public string STT { get; set; }
public string HVT { get; set; }
public string Age { get; set; }
public string State { get; set; }
}
private void button1_Click(object sender, RoutedEventArgs e)
{
List<person> people = new List<person>();
people.Add(new person() { STT = textBox1.Text.ToString(), HVT = textBox2.Text.ToString(), Age = textBox3.Text.ToString(), State = textBox4.Text.ToString() });
this.listView1.ItemsSource = people;
}
}
}
单击“添加”按钮后,程序将添加包含人员信息的行。但是我不能在第1行之后添加第2行,第3行...... 请帮忙,谢谢!
答案 0 :(得分:0)
您应该使用可观察的集合作为源,这样您每次都不需要设置项目来源。可观察集合将在更改时通知,因此UI将响应列表的新添加。
答案 1 :(得分:0)
试试这个 - &gt;
public partial class MainWindow : Window
{
private ObservableCollection<person> _people;
public ObservableCollection<person> people
{
get { return _people; }
set { _people = value; }
}
public MainWindow()
{
InitializeComponent();
people = new ObservableCollection<person>();
}
public class person
{
public string STT { get; set; }
public string HVT { get; set; }
public string Age { get; set; }
public string State { get; set; }
}
public void button1_Click(object sender, RoutedEventArgs e)
{
people.Add(new person() { STT = textBox1.Text.ToString(), HVT = textBox2.Text.ToString(), Age = textBox3.Text.ToString(), State = textBox4.Text.ToString() });
this.listView1.ItemsSource = people;
}
}
这将有效..:)
答案 2 :(得分:0)
只需将listview绑定到ObservableCollection,它就会自动更新列表的内容。
<ListView ItemsSource="{Binding Persons}">
...
</ListView>
并在ViewModel(或后面的代码)中声明属性Persons:
Public ObservableCollection<Person> Persons
{
...
}
诀窍是ObservableCollection实现了INotifyCollectionChanged,因此它跟踪从集合中添加或删除项目的时间并更新源。