Is there any way to refresh only one column of the listview?
Listview1.Items.Refresh()
refreshes whole listview but I want to refresh only one column
This is a sample for updating single properties in a ListView.
The window (contains a ListView
and a Button
).
<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>
The ViewModel for the window
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");
}
}
}
The ViewModel for an item in the ListView
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");
}
}
}
In the view you have ListView
with two TextBlock
for each item. When you press the button the property First
of the first item will be updated. Maybe this sample code will help you figure out your problem. RelayCommand
is my implementation for ICommand
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.