C# 在 WPF 中刷新 ListBox 的简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14096414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EASY way to refresh ListBox in WPF?
提问by Fengson
I have created a simple form that inserts/updates/deletes a values for Northwind Customers. Everything works fine, except in order to see a results, I have to close it, and reopen again. My form looks like this :
我创建了一个简单的表单,可以为 Northwind 客户插入/更新/删除值。一切正常,除了为了查看结果,我必须关闭它,然后重新打开。我的表格是这样的:
I've searched tens of articles on how to refresh ListBox
, but all of those use interface implementing, or using DataSets, and stuff I have never heard of and cannot implement. It's a very simple project, using simple procedures. Is there an easy way to refresh the list of customers without adding many lines of code?
我搜索了几十篇关于如何刷新的文章ListBox
,但所有这些都使用接口实现,或者使用数据集,以及我从未听说过并且无法实现的东西。这是一个非常简单的项目,使用简单的程序。有没有一种简单的方法可以在不添加多行代码的情况下刷新客户列表?
采纳答案by user1610015
How about calling ListBox.UpdateLayout?
调用 ListBox.UpdateLayout 怎么样?
Of course you also need to update the particular item(s) so that it returns the updated string from the ToString method.
当然,您还需要更新特定项目,以便它从 ToString 方法返回更新后的字符串。
UPDATE:I think you also need to call ListBox.InvalidateArrange before you call ListBox.UpdateLayout.
更新:我认为您还需要在调用 ListBox.UpdateLayout 之前调用 ListBox.InvalidateArrange。
回答by sa_ddam213
Are you using ObservableCollection
and does your model implement INotifyPropertyChanged
these two things will automaticly update the ListBox on any change. no need to explicitly refresh the list.
您是否正在使用ObservableCollection
并且您的模型是否实现了INotifyPropertyChanged
这两件事将在任何更改时自动更新 ListBox。无需显式刷新列表。
Here is a small example of using ObservableCollection
and INotifyPropertyChanged
, obviously you will populate your ObservableCollection from your SQL
database.
这是一个使用ObservableCollection
and的小例子INotifyPropertyChanged
,显然你将从你的SQL
数据库中填充你的 ObservableCollection 。
Window:
窗户:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private ObservableCollection<MyModel> _list = new ObservableCollection<MyModel>();
private MyModel _selectedModel;
public MainWindow()
{
InitializeComponent();
List.Add(new MyModel { Name = "James", CompanyName = "StackOverflow"});
List.Add(new MyModel { Name = "Adam", CompanyName = "StackOverflow" });
List.Add(new MyModel { Name = "Chris", CompanyName = "StackOverflow" });
List.Add(new MyModel { Name = "Steve", CompanyName = "StackOverflow" });
List.Add(new MyModel { Name = "Brent", CompanyName = "StackOverflow" });
}
public ObservableCollection<MyModel> List
{
get { return _list; }
set { _list = value; }
}
public MyModel SelectedModel
{
get { return _selectedModel; }
set { _selectedModel = value; NotifyPropertyChanged("SelectedModel"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
Xaml
xml
<Window x:Class="WpfApplication11.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Name="UI">
<Grid>
<ListBox ItemsSource="{Binding ElementName=UI, Path=List}" SelectedItem="{Binding ElementName=UI, Path=SelectedModel}" Margin="0,0,200,0" DisplayMemberPath="DisplayMember" SelectedIndex="0" />
<StackPanel HorizontalAlignment="Left" Height="100" Margin="322,10,0,0" VerticalAlignment="Top" Width="185">
<TextBlock Text="Name" />
<TextBox Height="23" TextWrapping="Wrap" Text="{Binding ElementName=UI, Path=SelectedModel.Name, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="Company Name" />
<TextBox Height="23" TextWrapping="Wrap" Text="{Binding ElementName=UI, Path=SelectedModel.CompanyName, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</Grid>
</Window>
Model
模型
public class MyModel : INotifyPropertyChanged
{
private string _name;
private string _companyName;
public string Name
{
get { return _name; }
set { _name = value; NotifyPropertyChanged("Name"); }
}
public string CompanyName
{
get { return _companyName; }
set { _companyName = value; NotifyPropertyChanged("CompanyName"); }
}
public string DisplayMember
{
get { return string.Format("{0} ({1})", Name, CompanyName); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
PropertyChanged(this, new PropertyChangedEventArgs("DisplayMember"));
}
}
}
In this case any edit to properties will Update your list instantly, also will update when new Items are added/removed.
在这种情况下,对属性的任何编辑都会立即更新您的列表,也会在添加/删除新项目时更新。
回答by zezba9000
The simple answer is: myListBox.Items.Refresh();
简单的答案是: myListBox.Items.Refresh();