更改连接列表时,列表框不会更新

时间:2014-01-08 14:22:54

标签: c# wpf binding listbox

我在WPF中有一个列表框,我希望每次在代码中更改值时,列表的内容都会更改。

在我的程序开始时,我在ListBox中插入默认值,这很有效。

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;
    }

但是当我在这里更改值时,它不会影响ListBox

        //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。我在stackoverflow上找到了关于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;
    }
}

编辑:这里是我的ListBox的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?

1 个答案:

答案 0 :(得分:2)

您将_compareListItems1设置为List<CompareListItem>的新实例,但compareSelectionList1.ItemsSource仍然引用前一个实例。这就是为什么你需要重新分配ItemsSource才能使它工作的原因。

  

但我认为这不是DataBindings方法的意义

目前,您使用绑定来设置ItemsSource,因此无法自动刷新。为此,您需要将列表公开为属性,并在Window中实现INotifyPropertyChanged(另一个选项是将其作为依赖项属性公开)。在XAML中,将ItemsSource绑定到list属性,它将按预期工作。