2017-06-16 77 views
-1

我有一個顯示搜索框搜索結果的列表視圖。加載窗口時,數據庫中的所有數據都以listview顯示 - 「這很好」。ListView未在WPF中自動更新

但是,當我搜索的東西,視圖模型中的變量更改和存儲新的數據,但列表視圖不顯示來自變量的新數據。對於列表視圖

XAML代碼

<ListView x:Name="itemsListView" ScrollViewer.CanContentScroll="True" 
        ItemsSource="{Binding Rows, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" 
       Grid.Row="1" 
       HorizontalAlignment="Stretch" 
       VerticalAlignment="Stretch" 
       Margin="40,10,40,10" 
       FlowDirection="RightToLeft" IsSynchronizedWithCurrentItem="True" SelectedIndex="1"> 
    <ListView.View> 
     <GridView AllowsColumnReorder="False"> 
      <GridViewColumn DisplayMemberBinding="{Binding BankName}" 
          Width="250" 
          Header="bank name"/> 
      <GridViewColumn DisplayMemberBinding="{Binding Credit}" 
          Header="credit" 
          Width="100"/> 
      <GridViewColumn DisplayMemberBinding="{Binding IsActive}" 
          Header="statue" 
          Width="100"/> 
      <GridViewColumn DisplayMemberBinding="{Binding Notes}" 
          Header="notes" 
          Width="230"/> 
     </GridView> 
    </ListView.View> 
</ListView> 

視圖模型代碼

class BanksVM 
{ 
    private ObservableCollection<BanksListVM> m_Rows; 

    public ObservableCollection<BanksListVM> Rows 
    { 
     get { return m_Rows; } 
     set { m_Rows = value; } 
    } 

    private string _BanksSearchBox; 

    public string BanksSearchBox 
    { 
     get { return _BanksSearchBox; } 
     set { 
      _BanksSearchBox = value; 
      SearchInBanksMethod(); 
     } 
    } 

    //set all data to listview 
    public BanksVM() 
    { 
     Rows = new ObservableCollection<BanksListVM>(); 
     Rows.Clear(); 

     List <Banks> bankslist = new List<Banks>(); 
     Banks banks = new Banks(); 
     bankslist = banks.GetAllBanks(); 

     foreach (Banks b in bankslist) 
     { 
      string IsActive = ""; 

      if (b.IsActive == true) 
       IsActive = "open"; 
      else 
       IsActive = "closed"; 

      Rows.Add(new BanksListVM 
      { 
       ID = b.ID, 
       BankName = b.BankName, 
       IsActive = IsActive, 
       Credit = b.Credit, 
       Notes = b.Notes 
      }); 
     } 
    } 

    // refresh listview method 
    public void SearchInBanksMethod() { 
     Rows = new ObservableCollection<BanksListVM>(); 
     Rows.Clear(); 

     List<Banks> bankslist = new List<Banks>(); 
     search Search = new search(); 
     bankslist = Search.SearchInBanks(_BanksSearchBox); 

     foreach (Banks b in bankslist) 
     { 
      string IsActive = ""; 

      if (b.IsActive == true) 
       IsActive = "open"; 
      else 
       IsActive = "closed"; 

      Rows.Add(new BanksListVM 
      { 
       ID = b.ID, 
       BankName = b.BankName, 
       IsActive = IsActive, 
       Credit = b.Credit, 
       Notes = b.Notes 
      }); 
     } 
    } 
} 
+0

你並不需要,如果你有新的實例它來清除行集合。只能在「SearchInBanksMethod」中使用「清除」而不是分配新對象。還要確保BanksVM從INotifyPropertyChanged繼承。 –

+0

好的,但是這不是解決問題 –

+1

那應該解決問題。我建議使用 public ObservableCollection Rows {get;組; } = new ...(); 在這種情況下,你甚至不必在構造函數中設置它。 –

回答

1

當重新分配的ObservableCollection,結合不通知有關新的屬性值。

SearchInBanksMethod,刪除

Rows = new ObservableCollection<BanksListVM>(); 

,並且只使用

Rows.Clear();