Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改连接的列表时,列表框不会更新_C#_Wpf_Binding_Listbox - Fatal编程技术网

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

C# 更改连接的列表时,列表框不会更新,c#,wpf,binding,listbox,C#,Wpf,Binding,Listbox,我在WPF中有一个列表框,我希望每次在代码中更改值时列表的内容都会更改 在我的程序开始时,我在列表框中插入默认值,这很有效 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel; using System.Collections.ObjectModel; publ

我在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;
    }
我的列表框中的每一项都是比较项。我在stackoverflow上找到了以下主题,并在这里实现了它。当我更新列表中的单个对象时,它会起作用

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

如何替换列表,以便通过数据绑定将列表框更新为?

您正在将
\u compareListItems1
设置为
列表的新实例,但
compareSelectionList1.ItemsSource
仍然引用以前的实例。这就是为什么需要重新分配
ItemsSource
才能使其工作

但我认为这不是数据绑定方法的意义


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

我将在明天完成它,但我非常肯定这会工作,谢谢!否则我会再问一次。
// 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 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>
_compareListItems1 = ReloadCompareList(_dataTable1);    // ESSENTIAL LINE
compareSelectionList1.ItemsSource = _compareListItems1;