2010-12-05 109 views
0

我有一列中的文本框的列表視圖。 Listview綁定到IEnumerable集合。 當我編輯文本框中的文本並單擊確定時,綁定集合只有原始值。
我監聽列表視圖,看到在listviewitem的對象中出現在文本框中的變化,而列表視圖在屏幕上,但在確定按鈕處理程序,他們都走了。Wpf Listview綁定對象沒有變化

下面是窗口的XAML和代碼隱藏(收款人是窗口的調用者產生LINQ到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

`where!cbListedOnly.IsChecked || p.Listed` – SLaks 2010-12-05 04:33:42

回答

0

我發現這個問題。我用作listview的ItemSource Linq查詢,它在訪問時被刷新,失去了更改。我將ItemSource改爲List,在查詢中使用ToList(),現在它工作。