列表框单元格仅在滚动之后更新

时间:2012-11-08 19:01:49

标签: c# wpf xaml listview

我有一个简单的列表视图,带有网格控件。我使用observablecollection将对象绑定到网格。 inotifyPropertyChanged在setter上实现。

网格有三列。在按钮上单击我加载网格,在两列中包含一些数据行。然后用户点击另一个按钮,我也在网格的第三列中添加了一些文本。问题是这个新文本只在那些当前不在屏幕上的行中显示在网格中。如果我向下和向上滚动,其余部分一旦离开屏幕区域就会加载到滚动之外。

这可能是一个先发制人的问题,但我尝试了各种代码的排列,并搜索和阅读了文章但没有帮助。

XAML

<Window x:Class="MediaFolderCleanupTool.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="392" Width="582">
    <Grid Height="340" Width="550">
        <Button Content="Search" Height="23" HorizontalAlignment="Left" Margin="12,38,0,0" Name="btnSearch" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <ListView ItemTemplate="{Binding FileItem}" Height="198" HorizontalAlignment="Left" Margin="14,67,0,0" Name="lstTargetFiles" VerticalAlignment="Top" Width="524" >
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="" Width="40">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding IsSelected}" Name="Select" IsThreeState="False"  Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="File"
                      Width="350"
               DisplayMemberBinding="{Binding Path=Name}" />
                    <GridViewColumn Header="Status"
                      Width="100"
               DisplayMemberBinding="{Binding Path=Status}" />
                </GridView>
            </ListView.View>
        </ListView>
        <Button Content="Delete" Height="23" HorizontalAlignment="Left" IsEnabled="False" Margin="13,306,0,0" Name="btnDelete" VerticalAlignment="Top" Width="75" Click="btnDelete_Click_1" />
        <Label Height="28" HorizontalAlignment="Left" Margin="16,271,0,0" Name="lblCount" VerticalAlignment="Top" Width="371" />
    </Grid>
</Window>

点击屏幕上的按钮,下面会调用

private void DelFiles(ObservableCollection<FileItem> files)
        {

            foreach (FileItem fi in files)
            {
                try
                {
                    fi.Status = "Deleted";
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.InnerException);
                    fi.Status = "Error Deleting";
                }
            }
        }

这是FileItem类

class FileItem : INotifyPropertyChanged
    {

        public const string NamePropertyName = "CheckBoxState";

        private bool _checkboxstate = true;

        public string Name { get; set; }
        public string Status { get; set; }
        public bool IsSelected
        {
            get
            {
                return _checkboxstate;
            }
            set
            {
                if (_checkboxstate == value)
                {
                    return;
                }
                _checkboxstate = value;
                OnPropertyChanged(NamePropertyName);
            }


        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

1 个答案:

答案 0 :(得分:0)

试试这个:

class FileItem : INotifyPropertyChanged
    {

        public const string NamePropertyName = "CheckBoxState";

        private bool _checkboxstate = true;

        public string Name { get; set; }

        string _status;     
        public string Status 
        { 
            get { return _status; } 
            set { _status = value; OnPropertyChanged("Status"); } 
        }

        public bool IsSelected
        {
            get
            {
                return _checkboxstate;
            }
            set
            {
                if (_checkboxstate == value)
                {
                    return;
                }
                _checkboxstate = value;
                OnPropertyChanged(NamePropertyName);
            }


        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
相关问题