我在一列中有文本框列表视图。 Listview绑定到IEnumerable集合。 当我在文本框中编辑文本并单击“确定”时,绑定集合仅包含原始值 我偷看了listview,看到listviewitem的对象中出现的更改出现在listview在屏幕上,但是在OK按钮处理程序中它们都消失了。
这是窗口的xaml和代码隐藏(收款人是由窗口调用者生成的linq-to-xml查询的结果):
<Grid>
<ListView HorizontalAlignment="Stretch" Margin="0,38,0,0" Name="lvPayee"
VerticalAlignment="Stretch" GridViewColumnHeader.Click="lvPayee_Click" Background="AliceBlue">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Listed" Width="60">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Margin="15,0,0,0" IsChecked="{Binding Listed}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Payee" Width="425">
<GridViewColumn.CellTemplate>
<DataTemplate DataType="{x:Type TextBox}">
<TextBox Width="420" Text="{Binding Name}" Background="Transparent"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Use Count" Width="80" DisplayMemberBinding="{Binding UseCount}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
<CheckBox Content="Listed Only" Height="27" HorizontalAlignment="Left" Margin="57,12,0,0" Name="cbListedOnly" VerticalAlignment="Top" Width="129" IsChecked="True" Click="Listed_Clicked" />
<Button Content="OK" Height="27" HorizontalAlignment="Left" Margin="381,6,0,0" Name="btnOK" VerticalAlignment="Top" Width="89" Click="OK_Clicked" />
<Button Content="Cancel" Height="27" HorizontalAlignment="Left" Margin="493,6,0,0" Name="btnCancel" VerticalAlignment="Top" Width="80" IsCancel="True" />
</Grid>
public partial class PayeeManager : Window
{
private IEnumerable<Payee> payees = null;
private IEnumerable<Payee> payto = null;
private bool reverse = false;
private string lastColumn = "";
public PayeeManager(Window owner, IEnumerable<Payee> payees)
{
this.Owner = owner;
this.payees = payees;
InitializeComponent();
payto = from p in this.payees
where p.Listed == true
orderby p.Name
select p;
lvPayee.ItemsSource = payto;
}
public class Payee
{
public string Name { get; set; }
public int UseCount { get; set; }
public bool Listed { get; set; }
public string OldName { get; set; }
public bool OldListed { get; set; }
}
private void Listed_Clicked(object sender, RoutedEventArgs e)
{
payto = from p in this.payees
where cbListedOnly.IsChecked == true ? p.Listed == true : true
select p;
lvPayee.ItemsSource = payto;
}
private void lvPayee_Click(object sender, RoutedEventArgs e)
{
if (!(e.OriginalSource is GridViewColumnHeader)) return;
string header = (string)((GridViewColumnHeader)e.OriginalSource).Column.Header;
if (lastColumn != header)
{
lastColumn = header;
reverse = false;
}
else reverse = !reverse;
switch (header)
{
case "Listed":
payto = from p in this.payees
where cbListedOnly.IsChecked == true ? p.Listed == true : 1 == 1
orderby p.Listed
select p;
break;
case "Payee":
payto = from p in this.payees
where cbListedOnly.IsChecked == true ? p.Listed == true : 1 == 1
orderby p.Name
select p;
break;
case "Use Count":
payto = from p in this.payees
where cbListedOnly.IsChecked == true ? p.Listed == true : 1 == 1
orderby p.UseCount
select p;
break;
default:
return;
}
if (reverse) payto = payto.Reverse();
lvPayee.ItemsSource = payto;
}
private void OK_Clicked(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
}
答案 0 :(得分:0)
我发现了问题。我使用listview ItemSsource Linq查询,并在访问时刷新,丢失更改。我使用ToList()将ItemSource更改为List,现在可以正常工作。