如何仅刷新listview

时间:2017-03-24 07:11:39

标签: c# listview refresh

有没有办法只刷新列表视图的一列?

Listview1.Items.Refresh()刷新整个列表视图,但我想只刷新一列

1 个答案:

答案 0 :(得分:0)

这是更新ListView中单个属性的示例。

窗口(包含ListViewButton)。

<StackPanel>
    <ListView ItemsSource="{Binding Items}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100" />
                        <ColumnDefinition Width="100" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0"
                               Text="{Binding First}" />
                    <TextBlock Grid.Column="1"
                               Text="{Binding Second}" />
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <Button Command="{Binding DoSomethingCommand}"
            Content="Do something" />
</StackPanel>

窗口的ViewModel

public class MainViewModel : INotifyPropertyChanged
{
    public RelayCommand DoSomethingCommand { get; set; }

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

    public MainViewModel()
    {
        Items = new ObservableCollection<ItemViewModel>
        {
            new ItemViewModel { First = "eins", Second = "zwei" },
            new ItemViewModel { First = "drei", Second = "vier" },
            new ItemViewModel { First = "fünf", Second = "sechs" }
        };

        DoSomethingCommand = new RelayCommand(OnDoSomething);
    }

    private void OnDoSomething()
    {
        Items.First().First = "sieben";
    }

    ObservableCollection<ItemViewModel> _items;
    public ObservableCollection<ItemViewModel> Items
    {
        get
        {
            return _items;
        }

        set
        {
            _items = value;
            OnPropertyChanged("Items");
        }
    }
}

ListView

中项目的ViewModel
public class ItemViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    string _first;
    public string First
    {
        get
        {
            return _first;
        }

        set
        {
            _first = value;
            OnPropertyChanged("First");
        }
    }

    string _second;
    public string Second
    {
        get
        {
            return _second;
        }

        set
        {
            _second = value;
            OnPropertyChanged("Second");
        }
    }
}

在视图中,每个项目ListView有两个TextBlock。按下按钮时,将更新第一个项目的属性First。也许这个示例代码可以帮助您找出问题所在。 RelayCommand是我ICommand的实现。