Last active
May 5, 2019 04:21
-
-
Save giobel/311d62164b6edac8b146affdb0940a30 to your computer and use it in GitHub Desktop.
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
Credits: fcamuso |
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="HelloWpf.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:HelloWpf" | |
xmlns:sys="clr-namespace:System;assembly=mscorlib" | |
mc:Ignorable="d" | |
Title="MainWindow" Height="300" Width="500"> | |
<Window.Resources> | |
</Window.Resources> | |
<StackPanel Margin="10" > | |
<StackPanel Orientation="Horizontal"> | |
<TextBlock Text="Nome:" /> | |
<TextBox x:Name="tboxName" Width="120" Margin="8,0"/> | |
<TextBlock Text="Cognome:" /> | |
<TextBox x:Name="tboxSurname" Width="120" Margin="8,0"/> | |
<Button x:Name="btnSalutami" Click="BtnSalutami_Click" Content="Salutami" /> | |
</StackPanel> | |
<TextBlock/> | |
<TextBlock x:Name="tboxResult" x:FieldModifier="private"/> | |
</StackPanel> | |
</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
namespace HelloWpf | |
{ | |
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
} | |
private void BtnSalutami_Click(object sender, RoutedEventArgs e) | |
{ | |
SalutaMi(); | |
} | |
private void SalutaMi() | |
{ | |
tboxResult.Text = String.Format("Ciao {0} {1}", tboxName.Text, tboxSurname.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
<Window x:Class="HelloWpf.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:HelloWpf" | |
mc:Ignorable="d" | |
Title="MainWindow" Height="135" Width="367"> | |
<StackPanel > | |
<StackPanel Orientation="Horizontal" Margin="10,10,10,0"> | |
<StackPanel Orientation="Horizontal" Background="Aquamarine" Margin="0,0,5,0"> | |
<Label>Surname</Label> | |
<ComboBox x:Name="comboSurname" Width="150" Margin="3,3,10,3" /> | |
</StackPanel> | |
<Button Content="Salutami" Width="105" Click="Button_Click"/> | |
</StackPanel> | |
<StackPanel Background="Beige" Height="50" Margin="10,10,10,0"> | |
<TextBox x:Name="tboxResult" Margin=" 10,10,0,0" Width="150" Background="Transparent" HorizontalAlignment="Center" BorderBrush="{x:Null}"/> | |
</StackPanel> | |
</StackPanel> | |
</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
public partial class MainWindow : Window | |
{ | |
private Models.PeopleService peopleService = null; | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
peopleService = new Models.PeopleService(); | |
comboSurname.ItemsSource = peopleService.People; | |
comboSurname.DisplayMemberPath = "Surname"; | |
} | |
private void BtnSalutami_Click(object sender, RoutedEventArgs e) | |
{ | |
SalutaMi(); | |
} | |
private void SalutaMi() | |
{ | |
if (comboSurname.SelectedItem != null) | |
{ | |
Models.Person chosenPerson = comboSurname.SelectedItem as Models.Person; | |
tboxResult.Text = String.Format("Ciao {0} {1}", chosenPerson.Name, chosenPerson.Surname); | |
} | |
} | |
} |
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
class PeopleService | |
{ | |
private List<Person> _people = null; //private -> need a getter to make it public | |
public PeopleService() | |
{ | |
_people = new List<Person>(); | |
_people.Add(new Person { Name = "Mark", Surname = "Gallagher" }); | |
_people.Add(new Person { Name = "John", Surname = "Snow" }); | |
} | |
public IList<Person> People => _people; //return an interface (more generic) | |
/* equivalent to: | |
public IList<Person> People | |
{ | |
get { return _people; } | |
}*/ | |
} |
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
public class Person | |
{ | |
public string Name { get; set; } | |
public string Surname { 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
public partial class App : Application | |
{/* | |
private void Application_Startup(object sender, StartupEventArgs e) | |
{ | |
MainWindow mwin = new MainWindow(new Models.PeopleService()); | |
mwin.Show(); | |
}*/ | |
protected override void OnStartup(StartupEventArgs e) | |
{ | |
base.OnStartup(e); | |
MainWindow mainWindow = new MainWindow(new Models.PeopleService()); | |
MainWindow.Show(); | |
} | |
} |
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
public partial class MainWindow : Window | |
{ | |
private Models.IPeopleService peopleService = null; | |
//private Models.PeopleService peopleService = null; | |
public MainWindow(Models.IPeopleService _peopleService) | |
{ | |
InitializeComponent(); | |
//peopleService = new Models.PeopleService(); | |
peopleService = _peopleService; | |
comboSurname.ItemsSource = peopleService.People; | |
comboSurname.DisplayMemberPath = "Surname"; | |
} | |
private void BtnSalutami_Click(object sender, RoutedEventArgs e) | |
{ | |
SalutaMi(); | |
} | |
private void SalutaMi() | |
{ | |
if (comboSurname.SelectedItem != null) | |
{ | |
Models.Person chosenPerson = comboSurname.SelectedItem as Models.Person; | |
tboxResult.Text = String.Format("Ciao {0} {1}", chosenPerson.Name, chosenPerson.Surname); | |
} | |
} | |
} |
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
public partial class App : Application | |
{ | |
protected override void OnStartup(StartupEventArgs e) | |
{ | |
Models.IPeopleService peopleService = new Models.PeopleService(); | |
ViewModels.MWViewModel viewModel = new ViewModels.MWViewModel(peopleService); | |
MainWindow mw = new MainWindow(viewModel); | |
mw.Show(); | |
} | |
} |
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
public partial class MainWindow : Window | |
{ | |
public MainWindow(ViewModels.MWViewModel vM) | |
{ | |
InitializeComponent(); | |
this.DataContext = vM; | |
} | |
private void Button_Click(object sender, RoutedEventArgs e) | |
{ | |
(DataContext as ViewModels.MWViewModel).SalutaMi(); | |
} | |
} |
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="HelloWpf.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:HelloWpf" | |
mc:Ignorable="d" | |
Title="MainWindow" Height="140" Width="380"> | |
<Grid ShowGridLines="False" Margin="3"> | |
<Grid.ColumnDefinitions> | |
<ColumnDefinition Width="1*"/> | |
<ColumnDefinition Width="0.5*"/> | |
</Grid.ColumnDefinitions> | |
<Grid.RowDefinitions> | |
<RowDefinition/> | |
<RowDefinition/> | |
</Grid.RowDefinitions> | |
<StackPanel Orientation="Horizontal" Background="Bisque" Margin="5"> | |
<Label Content="Surname" VerticalAlignment="Center"/> | |
<ComboBox Width="170" Height="25" | |
x:Name="cbox" | |
ItemsSource="{Binding People}" | |
DisplayMemberPath="Surname" | |
SelectedItem="{Binding Path = ChosenP}" | |
/> | |
</StackPanel> | |
<Button Grid.Column="1" Width="100" Height="25" Content="Salutami" Click="Button_Click"/> | |
<StackPanel Grid.Row="1" Background="PaleGreen" Grid.ColumnSpan="2" Margin="10" Width=" 150"> | |
<TextBox x:Name="tbox" FontSize="15" TextAlignment="Center" Width="150" BorderBrush="Transparent" Background="Transparent" | |
Text="{Binding Path = TextSaluto}"/> | |
</StackPanel> | |
</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.Collections.Generic; | |
using System.ComponentModel; | |
namespace HelloWpf.ViewModels | |
{ | |
public class MWViewModel : INotifyPropertyChanged | |
{ | |
private Models.IPeopleService _peopleService = null; | |
public MWViewModel(Models.IPeopleService peopleService) | |
{ | |
_peopleService = peopleService; | |
} | |
public IList<Models.Person> People => _peopleService.PeopleList; | |
//properties for binding | |
public Models.Person ChosenP { get; set; } | |
private string _txtSaluto; | |
public event PropertyChangedEventHandler PropertyChanged; | |
public string TextSaluto | |
{ | |
get { return _txtSaluto; } | |
set | |
{ | |
_txtSaluto = value; | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TextSaluto))); //better to use nameof(Property) | |
} | |
} | |
public void SalutaMi() | |
{ | |
if (ChosenP != null) | |
{ | |
TextSaluto = string.Format("Ciao {0} {1}", ChosenP.Name, ChosenP.Surname); | |
} | |
} | |
} | |
} |
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.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace HelloWpf.Models | |
{ | |
public interface IPeopleService | |
{ | |
IList<Person> PeopleList { get; } | |
} | |
public class PeopleService : IPeopleService | |
{ | |
private List<Person> _peopleList = new List<Person>(); | |
public PeopleService() | |
{ | |
_peopleList.Add(new Person { Name = "Pablo", Surname = "Luis" }); | |
_peopleList.Add(new Person { Name = "Ana", Surname = "Maria" }); | |
_peopleList.Add(new Person { Name = "Sara", Surname = "Lucia" }); | |
} | |
public IList<Person> PeopleList => _peopleList; | |
} | |
} |
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.Collections.Generic; | |
using System.ComponentModel; | |
using System.Runtime.CompilerServices; | |
using System.Windows.Input; | |
namespace HelloWpf.ViewModels | |
{ | |
public abstract class BaseViewModel : INotifyPropertyChanged | |
{ | |
public event PropertyChangedEventHandler PropertyChanged; | |
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "") | |
{ | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); //better than using the string for refactoring | |
} | |
} | |
public class RelayCommand : ICommand | |
{ | |
public event EventHandler CanExecuteChanged; | |
private Action<object> executeMethod; | |
private Predicate<object> canExecuteMethod; | |
public RelayCommand(Action<object> Execute, Predicate<object> CanExecute) | |
{ | |
canExecuteMethod = CanExecute; | |
executeMethod = Execute; | |
} | |
public bool CanExecute(object parameter) | |
{ | |
return (canExecuteMethod == null) ? true : canExecuteMethod.Invoke(parameter); | |
} | |
public void Execute(object parameter) | |
{ | |
executeMethod.Invoke(parameter); | |
} | |
public void RaiseCanExecuteChanged() | |
{ | |
CanExecuteChanged?.Invoke(this, EventArgs.Empty); | |
} | |
} | |
public class MWViewModel : BaseViewModel | |
{ | |
private Models.IPeopleService _peopleService = null; | |
public IList<Models.Person> People => _peopleService.PeopleList; | |
public ViewModels.RelayCommand SalutaCommand { get; private set; } //private set -> read only | |
private Models.Person _chosenP; | |
public Models.Person ChosenP | |
{ | |
get | |
{ | |
return _chosenP; | |
} | |
set | |
{ | |
if (_chosenP == value) return; | |
_chosenP = value; | |
NotifyPropertyChanged(); | |
SalutaCommand.RaiseCanExecuteChanged(); | |
} | |
} | |
private string _txtSaluto; | |
public string TextSaluto | |
{ | |
get { return _txtSaluto; } | |
set | |
{ | |
_txtSaluto = value; | |
NotifyPropertyChanged(); | |
} | |
} | |
public void SalutaMi() | |
{ | |
if (ChosenP != null) | |
{ | |
TextSaluto = string.Format("Ciao {0} {1}", ChosenP.Name, ChosenP.Surname); | |
} | |
} | |
public MWViewModel(Models.IPeopleService peopleService) | |
{ | |
_peopleService = peopleService; | |
SalutaCommand = new RelayCommand(salutaMethod, salutaExecute); | |
} | |
private bool salutaExecute(object obj) | |
{ | |
return ChosenP != null; | |
} | |
private void salutaMethod(object obj) | |
{ | |
SalutaMi(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment