Skip to content

Instantly share code, notes, and snippets.

@RupertAvery
Last active September 4, 2022 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RupertAvery/213c939651e0bef7612b2f910909a21d to your computer and use it in GitHub Desktop.
Save RupertAvery/213c939651e0bef7612b2f910909a21d to your computer and use it in GitHub Desktop.
WPF ListView Binding
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace WPFSample
{
public class BaseNotifyModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
protected bool SetProperty<T>(ref T field, T newValue, [CallerMemberName] string propertyName = null)
{
if (!Equals(field, newValue))
{
field = newValue;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
return true;
}
return false;
}
}
}
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace WPFSample
{
public class Controller
{
private Model model;
public Controller (Model model)
{
this.model = model;
model.Items = new ObservableCollection<Item>();
model.Add = new RelayCommand(Add);
model.Modify = new RelayCommand(Modify);
}
public void Add(object obj)
{
model.Items.Add(new Item() { Text = $"Item {model.Items.Count + 1}" });
}
public void Modify(object obj)
{
foreach (var item in model.Items)
{
item.Text = "Updated " + item.Text;
}
}
}
}
namespace WPFSample
{
public class Item : BaseNotifyModel
{
private string text;
public string Text {
get => text;
set => SetProperty(ref text, value);
}
}
}
<Window x:Class="WPFSample.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:WPFSample"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance local:Model, IsDesignTimeCreatable=True}"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Command="{Binding Add}">Add</Button>
<Button Grid.Row="1" Command="{Binding Modify}">Modify</Button>
<ListView Grid.Row="2" ItemsSource="{Binding Items}">
<ListView.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Text}"></Label>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>
using System.Windows;
namespace WPFSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Model model;
private Controller controller;
public MainWindow()
{
InitializeComponent();
model = new Model();
controller = new Controller(model);
DataContext = model;
}
}
}
using System.Collections.ObjectModel;
using System.Windows.Input;
namespace WPFSample
{
public class Model : BaseNotifyModel
{
public ICommand Add { get; set; }
public ICommand Modify { get; set; }
public ObservableCollection<Item> Items { get; set; }
}
}
using System;
using System.Windows.Input;
namespace WPFSample
{
public class RelayCommand : ICommand
{
private Action<object> execute;
private Func<object, bool> canExecute;
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return this.canExecute == null || this.canExecute(parameter);
}
public void Execute(object parameter)
{
this.execute(parameter);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment