2014-01-08 73 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

明天我会完成它,但我很肯定这会工作,谢谢!否则我会再问一次。 –