-
-
Save Protiguous/bd7876d1350714a1831f89bb289c194e to your computer and use it in GitHub Desktop.
WPF ListView Binding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace WPFSample | |
{ | |
public class Item : BaseNotifyModel | |
{ | |
private string text; | |
public string Text { | |
get => text; | |
set => SetProperty(ref text, value); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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