填充后的ListView不刷新

时间:2018-12-20 19:30:27

标签: c# wpf xaml listview data-binding

我正在使用填充有外部excel文件的ListView,正在读取作品,但此刻将近4天试图解决某些问题-ListView不会显示这些对象的更改状态。

在更改对象ListView中的值后,使用数据绑定设置具有自定义ListViewItem对象数组的ListView对象的ItemsSource属性后,

这是xaml代码

 <ListView Name="databaseListView" Height="333" Background="Silver">
                <ListView.View>
                    <GridView>
                        <GridView.Columns>
                        <GridViewColumn Width="50">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                        <CheckBox IsChecked="{Binding 
    Path=IsSelected, Mode=TwoWay}" Checked="CheckBox_Checked"></CheckBox>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding FirstName}" Width="100"/>
                        <GridViewColumn Header="LastName" DisplayMemberBinding="{Binding LastName}" Width="100"/>
                        <GridViewColumn Header="City" DisplayMemberBinding="{Binding City}" Width="100"/>
                        <GridViewColumn Header="Email" DisplayMemberBinding="{Binding Email}" Width="100"/>
                        <GridViewColumn Header="Personal Phone" DisplayMemberBinding="{Binding PersonalPhone}" Width="100"/>
                        <GridViewColumn Header="Call Status" DisplayMemberBinding="{Binding CallStatus}" Width="100"/>
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>   

这是自定义c#类:

class MyListViewItem
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string City { get; set; }
        public string Email { get; set; }
        public string PersonalPhone { get; set; }
        public string CallStatus { get; set; }
        public bool IsSelected { get; set; }
}

例如,此类代码不会影响ListView(假设ListView中选择的项目很少)

foreach(MyListViewItem item in databaseListView.SelectedItems){
   item.IsSelected = true;//or false also doesnt work
}

1 个答案:

答案 0 :(得分:1)

您将需要修改您的获取器/设置器,以触发INotifyPropertyChanged界面的PropertyChanged事件。

即,将public bool IsSelected { get; set; }更改为:

class MyListViewItem : INotifyPropertyChanged
{
    private bool isSelected;
    public bool IsSelected
    {
        get { return isSelected; }
        set
        {
            isSelected = value;
            OnPropertyChanged(nameof(IsSelected));
        }
    }
}

然后将以下函数添加到您的MyListViewItem类中

protected void OnPropertyChanged(string propertyName)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}