2014-01-08 72 views
0

我在WPF中有一個列表框,我希望列表的內容每次在代碼中更改值時都會更改。當我更改連接列表時,列表框不會更新

在我的程序開始時,我在默認值列表框中插入,這很好。

using System; using System.Collections.Generic; using System.Linq; 
using System.Text; using System.Threading.Tasks; using System.ComponentModel; 
using System.Collections.ObjectModel; 

public partial class MainWindow : Window 
{ 
    //here is my data which goes into the list 
    private DataTable _dataTable1 = null; 
    //list which goes into ListBox 
    private List<CompareListItem> _compareListItems1 = null; 
    public MainWindow() 
    { 
     InitializeComponent(); 
     // ..Code missing which writes data in _dataTable 
     //ReloadCompareList fills the _compareListItems1 with data from _dataTable1 
     _compareListItems1 = ReloadCompareList(_dataTable1); 
     //here I do the binding to the ListBox 
     compareSelectionList1.ItemsSource = _compareListItems1; 
    } 

但是,當我在這裏改變數值不影響列表框

 //this method is called when i want to replace the entire _compareListItems1 list 
     private void tableList1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      //this method write my data into _dataTable 
      _dataTable1 = ReloadTableList(tableList1, _dataTable1, tableGrid1); 
      if (_dataTable1 != null) 
      { 
       //the values of my compare list are replaced, but nothing happens with the ListBox 
       _compareListItems1 = ReloadCompareList(_dataTable1); // ESSENTIAL LINE 

      } 
     } 
} 

在我的ListBox中的每個項目將是一個CompareListItem。我在這裏找到了關於INotifyPropertyChanged的以下主題,我在這裏實現了這一點。它在我更新列表中的單個對象時起作用。

// Class for the items displayed in the Listbox of the compare list 
public class CompareListItem : INotifyPropertyChanged 
{ 
    private string itemTitle; 
    public string ItemTitle 
    { 
     get{return itemTitle;} 
     set{ 
      //this works when a single value in the list is changed, but not if i add or delete someting 
      SetField(ref itemTitle, value, "ItemTitle"); 
     } 
    } 

    public CompareListItem(string title) { 
     //does not affect the data bindings, could be "itemTitle = title;" to 
     SetField(ref itemTitle, title, "ItemTitle"); 
    } 
    //this is from https://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist 
    public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    protected bool SetField<T>(ref T field, T value, string propertyName) 
    { 
     if (EqualityComparer<T>.Default.Equals(field, value)) 
      return false; 
     field = value; 
     OnPropertyChanged(propertyName); 
     return true; 
    } 
} 

編輯:我的列表框,這裏的XAML:

<ListBox x:Name="compareSelectionList1" Margin="10,0,10,10" IsSynchronizedWithCurrentItem="False" Grid.Row="1" Height="100" VerticalAlignment="Bottom" SelectionMode="Multiple"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid Margin="0,2"> 
       <TextBlock Text="{Binding ItemTitle, Mode=TwoWay, diag:PresentationTraceSources.TraceLevel=High}"></TextBlock> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

當我添加以下行,將工作。但我認爲這不是DataBindings方法的含義。我理解這種方法就像「你在代碼中提到view-element一次,然後再也不會使用名稱」compareSelectionList1「。」

_compareListItems1 = ReloadCompareList(_dataTable1); // ESSENTIAL LINE 
compareSelectionList1.ItemsSource = _compareListItems1; 

我該如何替換我的列表,以便通過數據綁定更新ListBox?

回答

2

您正在將_compareListItems1設置爲List<CompareListItem>的新實例,但compareSelectionList1.ItemsSource仍指先前的實例。這就是爲什麼你需要重新分配ItemsSource才能工作。

但我認爲這是不與數據綁定

目前該方法的含義,您使用綁定設置ItemsSource,所以它不能自動刷新是 。爲此,您需要將列表公開爲屬性,並在窗口中實現INotifyPropertyChanged(另一個選項是將其公開爲依賴項屬性)。在XAML中,將ItemsSource綁定到列表屬性,並且它將按預期工作。

+0

明天我會完成它,但我很肯定這會工作,謝謝!否則我會再問一次。 –