Cod C# si XAML (Separarea interfetei grafice de logica aplicatiei)

MainWindow.xaml.cs

namespace SampleWpf
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}



MainWindowViewModel.cs

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;

namespace SampleWpf
{
    internal class MainWindowViewModel : INotifyPropertyChanged
    {
        public MainWindowViewModel() { Mesaje = new ObservableCollection<string>(); }

        public string UltimulMesaj
        {
            get { return _ultimulMesaj; }
            private set { _ultimulMesaj = value; InvokePropertyChanged("UltimulMesaj"); }
        }
        private string _ultimulMesaj;

        public string Mesaj
        {
           get { return _mesaj; }
           set { _mesaj = value; InvokePropertyChanged("Mesaj"); }
        }
        private string _mesaj;

        public ObservableCollection<string> Mesaje { get; private set; }

        public ICommand AdaugaMesajCommand
        {
            get
            {
                return _adaugaMesajCommand ?? (_adaugaMesajCommand = new Command(Execute));
            }
        }
        private ICommand _adaugaMesajCommand;

        private void Execute(object commandParam)
        {
            var mesaj = commandParam as string;
            Mesaje.Add(mesaj);
            UltimulMesaj = string.Format("Mesaj adaugat: {0}", mesaj);
            Mesaj = string.Empty;
        }

        // Implementeaza membru INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void InvokePropertyChanged(string propertyName)
        {
           var h = PropertyChanged;
            if (h != null)
                h(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}



Command.cs

using System;
using System.Windows.Input;

namespace SampleWpf
{
    internal class Command : ICommand
    {
        private readonly Action<object> _execute;

        public Command(Action<object> execute)
        {
            _execute = execute;
        }

        public bool CanExecute(object parameter) { return true; }
        public event EventHandler CanExecuteChanged;
        public void Execute(object parameter) { _execute(parameter); }
    }
}



MainWindow.xaml

<Window x:Class="SampleWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns
:viewModel="clr-namespace:SampleWpf"
        Title="Prima mea aplicatie" SizeToContent="WidthAndHeight" Background="SkyBlue" >
    <Window.DataContext>
        <viewModel:MainWindowViewModel/>
    </Window.DataContext>
    <StackPanel>
        <TextBlock x:Name="TbMesaj" Margin="4" Text
="{Binding UltimulMesaj}" />
        <ListBox x:Name="LstMesaje" Height="200" Width="300" Margin="4" ItemsSource="{Binding Mesaje}" />
        <StackPanel Orientation="Horizontal">
            <TextBox x:Name="TxtMesaj" Width="200" Margin="4"
 Text="{Binding Mesaj}" />
            <Button Content="Adauga mesaj" Margin="4"
                         Command="{Binding AdaugaMesajCommand}"
                         CommandParameter="{Binding Text, ElementName=TxtMesaj}" />
        </StackPanel>
    </StackPanel>
</Window>



Inapoi