2012-11-08 22 views
1

我有一個簡單的listview和一個網格控件。我使用observablecollection將一個對象綁定到了網格。 inotifyPropertyChanged在setter上實現。列表框單元格只能在滾動條之外更新

該網格有三列。在一個按鈕上單擊,我將兩列中的一些數據加載到網格中。然後用戶點擊另一個按鈕,我也在網格的第三列添加一些文本。問題是這個新文本只會顯示在網格上,而不會顯示在屏幕上。如果我向下滾動,其他人一旦離開滾動屏幕,屏幕區域就會加載。

這可能是一個首要的問題,但我嘗試過我的代碼的各種排列,並且搜索和閱讀文章,但沒有任何幫助。

XAML

<Window x:Class="MediaFolderCleanupTool.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="392" Width="582"> 
    <Grid Height="340" Width="550"> 
     <Button Content="Search" Height="23" HorizontalAlignment="Left" Margin="12,38,0,0" Name="btnSearch" VerticalAlignment="Top" Width="75" Click="button1_Click" /> 
     <ListView ItemTemplate="{Binding FileItem}" Height="198" HorizontalAlignment="Left" Margin="14,67,0,0" Name="lstTargetFiles" VerticalAlignment="Top" Width="524" > 
      <ListView.View> 
       <GridView> 
        <GridViewColumn Header="" Width="40"> 
         <GridViewColumn.CellTemplate> 
          <DataTemplate> 
           <CheckBox IsChecked="{Binding IsSelected}" Name="Select" IsThreeState="False" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" /> 
          </DataTemplate> 
         </GridViewColumn.CellTemplate> 
        </GridViewColumn> 
        <GridViewColumn Header="File" 
         Width="350" 
       DisplayMemberBinding="{Binding Path=Name}" /> 
        <GridViewColumn Header="Status" 
         Width="100" 
       DisplayMemberBinding="{Binding Path=Status}" /> 
       </GridView> 
      </ListView.View> 
     </ListView> 
     <Button Content="Delete" Height="23" HorizontalAlignment="Left" IsEnabled="False" Margin="13,306,0,0" Name="btnDelete" VerticalAlignment="Top" Width="75" Click="btnDelete_Click_1" /> 
     <Label Height="28" HorizontalAlignment="Left" Margin="16,271,0,0" Name="lblCount" VerticalAlignment="Top" Width="371" /> 
    </Grid> 
</Window> 

在屏幕上按一下按鈕,在下面被稱爲

private void DelFiles(ObservableCollection<FileItem> files) 
     { 

      foreach (FileItem fi in files) 
      { 
       try 
       { 
        fi.Status = "Deleted"; 
       } 
       catch (Exception ex) 
       { 
        Console.WriteLine(ex.InnerException); 
        fi.Status = "Error Deleting"; 
       } 
      } 
     } 

這裏是的FileItem類

class FileItem : INotifyPropertyChanged 
    { 

     public const string NamePropertyName = "CheckBoxState"; 

     private bool _checkboxstate = true; 

     public string Name { get; set; } 
     public string Status { get; set; } 
     public bool IsSelected 
     { 
      get 
      { 
       return _checkboxstate; 
      } 
      set 
      { 
       if (_checkboxstate == value) 
       { 
        return; 
       } 
       _checkboxstate = value; 
       OnPropertyChanged(NamePropertyName); 
      } 


     } 
     public event PropertyChangedEventHandler PropertyChanged; 
     protected void OnPropertyChanged(string propertyName) 
     { 
      if (this.PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
+0

而你的示例代碼說明了這個問題是....將被添加到你的問題? – slugster

+0

添加樣例代碼 –

+0

您能告訴我們'FileItem'類的源代碼嗎? – kmatyaszek

回答

0

試試這個:

class FileItem : INotifyPropertyChanged 
    { 

     public const string NamePropertyName = "CheckBoxState"; 

     private bool _checkboxstate = true; 

     public string Name { get; set; } 

     string _status;  
     public string Status 
     { 
      get { return _status; } 
      set { _status = value; OnPropertyChanged("Status"); } 
     } 

     public bool IsSelected 
     { 
      get 
      { 
       return _checkboxstate; 
      } 
      set 
      { 
       if (_checkboxstate == value) 
       { 
        return; 
       } 
       _checkboxstate = value; 
       OnPropertyChanged(NamePropertyName); 
      } 


     } 
     public event PropertyChangedEventHandler PropertyChanged; 
     protected void OnPropertyChanged(string propertyName) 
     { 
      if (this.PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
+0

謝謝......我錯過了OnPropertyChanged調用!!!下次不會忘記:p –

+0

不客氣:) – kmatyaszek