在WPF中刷新ListBox的简单方法?

时间:2012-12-31 01:09:48

标签: c# sql wpf listbox refresh

我创建了一个简单的表单,用于插入/更新/删除Northwind Customers的值。 一切正常,除了为了看到结果,我必须关闭它,然后重新打开。 我的表格如下:

enter image description here

我已经搜索了几篇关于如何刷新ListBox的文章,但所有这些文章都使用了接口实现,或使用DataSet,以及我从未听说过但无法实现的内容。这是一个非常简单的项目,使用简单的程序。是否有一种简单的方法可以刷新客户列表而无需添加多行代码?

4 个答案:

答案 0 :(得分:10)

简单的答案是:“myListBox.Items.Refresh();”

答案 1 :(得分:7)

您使用ObservableCollection并且您的模型实现INotifyPropertyChanged这两件事会在任何更改时自动更新ListBox。无需显式刷新列表。

以下是使用ObservableCollectionINotifyPropertyChanged的一个小示例,显然您将从SQL数据库中填充ObservableCollection。

窗口:

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

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

模型

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

在这种情况下,对属性的任何编辑都会立即更新您的列表,也会在添加/删除新项目时更新。

enter image description here

答案 2 :(得分:0)

如何调用ListBox.UpdateLayout?

当然,您还需要更新特定项目,以便从ToString方法返回更新的字符串。

更新:我认为您还需要在调用ListBox.UpdateLayout之前调用ListBox.InvalidateArrange。

答案 3 :(得分:0)

使用 INotifyPropertyChanged 是最好的方法,刷新整个列表不是一个好主意。
主入口:

public partial class MainWindow : Window
{
    private BindingList<FoodModel> foodList = new BindingList<FoodModel>();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button1_Click(object sender, RoutedEventArgs e)
    {
        foodList.Add(new FoodModel { foodName = "apple1" });
        foodList.Add(new FoodModel { foodName = "apple2" });
        foodList.Add(new FoodModel { foodName = "apple3" });
        FoodListBox.ItemsSource = foodList;
    }

    private void Button2_Click(object sender, RoutedEventArgs e)
    {
        foodList[0].foodName = "orange";
    }

    private void RefreshButton_Click(object sender, RoutedEventArgs e)
    {
        FoodListBox.Items.Refresh();
    }
}

型号:

public class FoodModel: INotifyPropertyChanged
{
    private string _foodName;

    public string foodName
    {
        get { return _foodName; }
        set
        {
            if (_foodName != value)
            {
                _foodName = value;
                PropertyChanged(this, new PropertyChangedEventArgs("foodName"));
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

XAML:

 <ListBox HorizontalAlignment="Center" Name="FoodListBox" VerticalAlignment="Top" Width="194" Height="150">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding foodName}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>