Partage
  • Partager sur Facebook
  • Partager sur Twitter

Evènement sur une Varibale C#

Sujet résolu
    22 février 2019 à 17:10:22

    Bonjour,

    J'aimerais faire une actualisation en directe d'une variable. Comme j'ai beaucoup de classe qui intéragisse sur la même varibale je souhaiterai créer un évèvement qui check quand la variable change, puis dès qu'elle change, un label change en directe affichant alors la nouvelle valeur de la variable.

    Comment est ce que je pourrais associer un EventHandler à une variable x ?

    • Partager sur Facebook
    • Partager sur Twitter
      22 février 2019 à 17:19:49

      Oui, ça se déclare "PropertyChangedEventHandler". Mais je sais pas comment l'utiliser malgré mes recherches
      • Partager sur Facebook
      • Partager sur Twitter
        22 février 2019 à 18:01:30

        Je t'ai fait un petit sample sur WPF :

        MainWindow.xaml.cs

        namespace WpfApp1
        {
            /// <summary>
            /// Interaction logic for MainWindow.xaml
            /// </summary>
            public partial class MainWindow
            {
                public MainWindow()
                {
                    InitializeComponent();
                }
        
                private void Button_Click(object sender, RoutedEventArgs e)
                {
                    this.MWVM.FirstName = this.TBChange.Text;
                }
            }
        }

        MainWindows.xaml

        <Window x:Class="WpfApp1.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:WpfApp1"
                mc:Ignorable="d"
                Title="MainWindow" Height="450" Width="800">
        
            <Window.DataContext>
                <local:MainWindowViewModel x:Name="MWVM"></local:MainWindowViewModel>
            </Window.DataContext>
        
        
            <Grid>
                <Label Content="Label" HorizontalAlignment="Left" Margin="62,172,0,0" VerticalAlignment="Top"/>
                <Label Content="Label" HorizontalAlignment="Left" Margin="62,219,0,0" VerticalAlignment="Top"/>
                <TextBox HorizontalAlignment="Left" Height="23" Margin="118,175,0,0" TextWrapping="Wrap" Text="{Binding FirstName}" VerticalAlignment="Top" Width="120"/>
                <TextBox HorizontalAlignment="Left" Height="23" Margin="118,219,0,0" TextWrapping="Wrap" Text="{Binding LastName}" VerticalAlignment="Top" Width="120"/>
                <Button Content="Button" HorizontalAlignment="Left" Margin="359,175,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
                <TextBox Name="TBChange" HorizontalAlignment="Left" Height="23" Margin="461,175,0,0" TextWrapping="Wrap" Text="{Binding LastName}" VerticalAlignment="Top" Width="120"/>
        
            </Grid>
        </Window>

        Mommy.cs

        public abstract class Mommy : INotifyPropertyChanged
            {
                public event PropertyChangedEventHandler PropertyChanged;
        
                protected void NotifyPropertyChanged(string name)
                {
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs(name));
                    }
                }
            }

        User.cs

        public class User : Mommy
            {
                private string _fisrtname;
                public string FirstName
                {
                    get
                    {
                        return _fisrtname;
                    }
                    set
                    {
                        _fisrtname = value;
                        NotifyPropertyChanged("FirstName");
                    }
                }
                private string _lastname;
                public string LastName
                {
                    get
                    {
                        return _lastname;
                    }
                    set
                    {
                        _lastname = value;
                        NotifyPropertyChanged("LastName");
                    }
                }
        
            }

        MainWindowViewModel.cs

        public class MainWindowViewModel : User
            {
                public MainWindowViewModel()
                {
                    FirstName = "New First name";
                    LastName = "New Last name";
                }
            }







        • Partager sur Facebook
        • Partager sur Twitter
          22 février 2019 à 18:42:09

          Code beaucoup trop complexe pour rien je trouve dans le sample,

          voici une solution simple :

          var tavariable = "texte"
          var memoire = tavariable;
          
          timer1_tick(...){ //créé un timer qui servira de boucle pour voir si ta variable a changée
          
          if(tavariable != memoire){
          //ici la variable a changée
          }
          memoire = tavariable;
          
          }



          • Partager sur Facebook
          • Partager sur Twitter
            22 février 2019 à 18:48:50

            Code faux surtout mais le forum est bugué donc je peux rien edit.

            Cependant :

            1) ça n'a rien de complexe. Les 3/4 c'est de l'auto généré.

            2) L'OP a écrit :

            Comme j'ai beaucoup de classe qui intéragisse sur la même varibale

            • Partager sur Facebook
            • Partager sur Twitter
              22 février 2019 à 20:05:10

              Merci, j'ai u peu plus compris lasyntaxe n'est pas claire pour moi.

              Est ce qu'il n'y aurait pas moyen de faire un Binding ?

              • Partager sur Facebook
              • Partager sur Twitter
                23 février 2019 à 3:02:47

                C'est le cas dans l'exemple que j'ai donné.

                Les TextBox ont leur propriété "Text" définie par {Binding=First/LastName}.

                <TextBox Text="{Binding FirstName}"/>
                <TextBox Text="{Binding LastName}" />


                La classe User hérite de la classe Mommy simplement pour la fonction "NotifyPropertyChanged".

                C'est plus intéressant de le mettre à part ne serait ce que pour ne pas avoir à le réécrire dans toute les classes qui en aurait potentiellement besoin.

                Une fois définie, toute ces classes n'ont qu'à hériter de la classe Mommy pour en profiter.

                • Partager sur Facebook
                • Partager sur Twitter

                Evènement sur une Varibale C#

                × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                • Editeur
                • Markdown