• 20 heures
  • Moyenne

Ce cours est visible gratuitement en ligne.

course.header.alt.is_video

course.header.alt.is_certifying

J'ai tout compris !

Mis à jour le 29/06/2017

Prenez en main le design pattern MVVM

Connectez-vous ou inscrivez-vous gratuitement pour bénéficier de toutes les fonctionnalités de ce cours !

Téléchargez le fichier d'audiodescription : Partie 3, Chapitre 4

Vous trouverez sous la vidéo les codes source du cours.

Code Source

Client.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PremiereUtilisationDUnDatacontext
{
    public class Client : INotifyPropertyChanged
    {
        private string nom;

        private string prenom;

        private string sexe;

        private int age;

        public event PropertyChangedEventHandler PropertyChanged;

        public string Nom
        {
            get
            {
                return nom;
            }

            set
            {
                if (value != nom)
                { 
                    nom = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Nom"));
                    }
                }
            }
        }

        public string Prenom
        {
            get
            {
                return prenom;
            }

            set
            {
                if (value != prenom)
                {
                    prenom = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Prenom"));
                    }
                }
            }
        }

        public string Sexe
        {
            get
            {
                return sexe;
            }

            set
            {
                if (value != sexe)
                {
                    sexe = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Sexe"));
                    }
                }
            }
        }

        public int Age
        {
            get
            {
                return age;
            }

            set
            {
                if (value != age)
                {
                    age = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Age"));
                    }
                }
            }
        }
    }
}

MainWindow.xaml

<Window x:Class="PremiereUtilisationDUnDatacontext.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:PremiereUtilisationDUnDatacontext"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:Client></local:Client>
    </Window.DataContext>
    <Grid>
        <Grid.Resources>
            <Style TargetType="Label">
                <Setter Property="HorizontalAlignment" Value="Center">
                </Setter>
                <Setter Property="VerticalAlignment" Value="Center">
                </Setter>
            </Style>
            <Style TargetType="TextBox">
                <Setter Property="HorizontalAlignment" Value="Stretch">
                </Setter>
                <Setter Property="VerticalAlignment" Value="Center">
                </Setter>
                <Setter Property="TextAlignment" Value="Center">
                </Setter>
            </Style>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition Width="5"/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center">Interface d'édition</Label>
        <Label Grid.Row="0" Grid.Column="3" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center">Fiche Client</Label>
        <Label Grid.Row="1" Grid.Column="0">Nom :</Label>
        <Label Grid.Row="2" Grid.Column="0">Prénom :</Label>
        <Label Grid.Row="3" Grid.Column="0">Âge :</Label>
        <Label Grid.Row="4" Grid.Column="0">Sexe :</Label>
        <Rectangle Grid.Column="2" Grid.RowSpan="5" Fill="Black"></Rectangle>
        <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Nom, Mode=TwoWay}"></TextBox>
        <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Prenom, Mode=TwoWay}"></TextBox>
        <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding Age, Mode=TwoWay}"></TextBox>
        <TextBox Grid.Row="4" Grid.Column="1" Text="{Binding Sexe, Mode=TwoWay}"></TextBox>
        <Label Grid.Row="1" Grid.Column="3">Nom :</Label>
        <Label Grid.Row="2" Grid.Column="3">Prénom :</Label>
        <Label Grid.Row="3" Grid.Column="3">Âge :</Label>
        <Label Grid.Row="4" Grid.Column="3">Sexe :</Label>
        <Label Grid.Row="1" Grid.Column="4" Content="{Binding Nom}"></Label>
        <Label Grid.Row="2" Grid.Column="4" Content="{Binding Prenom}"></Label>
        <Label Grid.Row="3" Grid.Column="4" Content="{Binding Age}"></Label>
        <Label Grid.Row="4" Grid.Column="4" Content="{Binding Sexe}"></Label>
        <Button Click="Button_Click" Grid.ColumnSpan="5" Grid.Row="5">RAZ</Button>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

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

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var c = (this.DataContext as Client);

            c.Age = 0;
            c.Nom = "";
            c.Prenom = "";
            c.Sexe = "";
        }
    }
}
Exemple de certificat de réussite
Exemple de certificat de réussite