Skip to content

Instantly share code, notes, and snippets.

@nickofc
Created May 18, 2018 09:10
Show Gist options
  • Save nickofc/6f88df7fcd9879b2962dd69c7f1c685c to your computer and use it in GitHub Desktop.
Save nickofc/6f88df7fcd9879b2962dd69c7f1c685c to your computer and use it in GitHub Desktop.
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Grid>
<ListView HorizontalAlignment="Left" Height="211" Margin="162,94,0,0" VerticalAlignment="Top" Width="414" ItemsSource="{Binding Path=Persons, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}"/>
</GridView>
</ListView.View>
</ListView>
<Button Content="Dodaj" Command="{Binding AddCommand}" HorizontalAlignment="Left" Margin="162,52,0,0" VerticalAlignment="Top" Width="75" Height="37"/>
<Button Content="Wyczyść" HorizontalAlignment="Left" Command="{Binding ClearCommand}" Margin="242,52,0,0" VerticalAlignment="Top" Width="75" Height="37"/>
<Label Content="{Binding SelectedItem.Name}" HorizontalAlignment="Left" Margin="162,310,0,0" VerticalAlignment="Top" Width="414"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
namespace WpfApp1
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public class ViewModel : INotifyPropertyChanged
{
public ObservableCollection<Person> Persons { get; } = new ObservableCollection<Person>();
private Person _previousPerson;
private Person _selectedItem;
public Person SelectedItem
{
get { return _selectedItem; }
set
{
if (value != null)
{
_previousPerson = value;
}
Set(ref _selectedItem, value);
}
}
public ICommand AddCommand { get; private set; }
public ICommand ClearCommand { get; private set; }
public ViewModel()
{
AddCommand = new Command(() =>
{
var names = new[]
{
"Damian",
"Adam",
"Michał",
"Piotrek",
"Agnieszka",
};
var random = new Random();
var person = new Person
{
Name = names[random.Next(names.Length)],
Age = random.Next(18, 60),
};
Persons.Add(person);
}, () => true);
ClearCommand = new Command(() => Persons.Clear(), () => true);
Persons.CollectionChanged += (sender, args) =>
{
if (args.Action == NotifyCollectionChangedAction.Add)
{
if (_previousPerson != null)
{
var item = Persons.Where(x => x.Name == _previousPerson.Name).FirstOrDefault();
if (item != null)
{
SelectedItem = item;
}
}
}
};
}
protected virtual bool Set<T>(ref T instance, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(instance, value))
return false;
instance = value;
OnPropertyChanged(propertyName);
return true;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment