【发布时间】:2012-12-25 04:56:24
【问题描述】:
我已阅读此主题: how to delete a specific text / message showed in a ListView ?
在winform中可以正常工作(我已经测试过了!)但是在WPF中就不行了。
我想在 listview1 中查找 firstnametxt 的文本并将其与 Firstname Column 项目进行比较,如果为真则删除该项目(包含该项目的行)
但我不知道如何在 WPF 中处理这个问题。
我的部分代码是这样的:
XAML:
<ListView Name="listView1 HorizontalAlignment="Left" VerticalAlignment="Top" />
<ListView.View>
<GridView>
<GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding Path=FirstName}" />
<GridViewColumn Header="LastName" DisplayMemberBinding="{Binding Path=LastName}" />
</GridView>
</ListView.View>
</ListView>
<TextBox Name="firstnametxt" Height="27" HorizontalAlignment="Left" Margin="271,20,0,0" VerticalAlignment="Top" Width="181" />
<Button Content="Add" Height="38" HorizontalAlignment="Left" Margin="310,242,0,0" Name="button1" VerticalAlignment="Top" Width="74" Click="button1_Click" />
<Button Content="Find" Height="38" HorizontalAlignment="Left" Name="button2" VerticalAlignment="Top" Width="74" Click="button2_Click" />
代码隐藏(C#):
public class Familiy
{
public Familiy(string firstname, string lastname)
{
this.FirstName = firstname;
this.LastName = lastname;
}
private string firstname;
public string FirstName
{
get { return firstname; }
set { firstname = value; }
}
private string lastname;
public string LastName
{
get { return lastname; }
set { lastname = value; }
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
///////////////////////////
}
【问题讨论】:
-
你是如何绑定列表的?它是在 XAML 中标记的还是在代码隐藏中完成的?
-
嗨彼得 O,我通过 xaml 绑定列表,如上所示,我将项目添加到列表中,如下所示:listView1.Items.Add(new Model("john","smith" ));
-
XAML 不显示您的绑定。或者我应该说绑定源。您最初从哪里获取列表的数据?它是代码隐藏中的
List吗?您是否使用对象数据源对其进行了标记? -
我知道你的意思,你的意思是一个数组列表,是吗?我知道,但我的代码在没有数组列表的情况下可以正常工作。
-
我正在尝试找出列表背后的结构 - 您的解决方案很容易通过利用绑定到列表的结构来解决 - 而循环遍历列表中的项目则更不理想.