【问题标题】:Listbox does not update when I change the connected list更改连接列表时列表框不更新
【发布时间】:2014-01-08 14:22:53
【问题描述】:

我在 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 方法的含义。我理解这样的方法,例如“您在代码中提到一次视图元素,然后您再也不会使用名称“compareSelectionList1”。”

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

如何替换我的列表,以便通过数据绑定更新 ListBox?

【问题讨论】:

    标签: c# wpf binding listbox


    【解决方案1】:

    您正在将_compareListItems1 设置为List&lt;CompareListItem&gt; 的新实例,但compareSelectionList1.ItemsSource 仍引用前一个实例。这就是为什么您需要重新分配 ItemsSource 才能使其工作的原因。

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

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

    【讨论】:

    • 我明天会完成它,但我很确定这会奏效,谢谢!否则我会在这里再问一次。
    猜你喜欢
    • 2016-11-02
    • 2010-10-20
    • 2013-03-02
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多