1

我有两个List<ColumnClass>。一个用于左侧列表视图,另一个用于右侧列表视图。这些列表视图在一个弹出框中。我正在修改两个 Listview 的列表,并再次将其分配给 Listview 的 ItemsSource。但这不会立即反映在 UI 中。当我关闭弹出窗口并再次打开时,它会反映更改。我错过了什么?

4

2 回答 2

2

您应该替换List<T>ObservableCollection<T>ObservableCollections 将在删除 Item 时更新您的 ListView ,如果您只是修改属性,请ColumnClass确保您的ColumnClass实现INotifyPropertyChanged这将允许 UI 在属性更改时更新。

例子:

代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MyColumns.Add(new ColumnClass { Name = "Column1" });
        MyColumns.Add(new ColumnClass { Name = "Column2" });
        MyColumns.Add(new ColumnClass { Name = "Column3" });
    }

    private ObservableCollection<ColumnClass> _myColumns = new ObservableCollection<ColumnClass>();
    public ObservableCollection<ColumnClass> MyColumns
    {
        get { return _myColumns; }
        set { _myColumns = value; }
    }
}

xml:

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WpfApplication8" Height="368" Width="486" Name="UI" >
    <Grid>
        <ListView ItemsSource="{Binding ElementName=UI, Path=MyColumns}" DisplayMemberPath="Name" />
    </Grid>
</Window>

模型:

public class ColumnClass : INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; NotifyPropertyChanged("Name"); }
    }



    public event PropertyChangedEventHandler PropertyChanged;
    /// <summary>
    /// Notifies the property changed.
    /// </summary>
    /// <param name="property">The info.</param>
    public void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}
于 2012-12-27T06:24:45.577 回答
0

您应该更改List<T>ObservableCollection<T>BindingList<T>

原因,List 没有实现INotifyPropertyChanged

于 2012-12-27T06:28:54.693 回答