IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

WPF - 2ème source de valeur


Sujet :

C#

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Août 2017
    Messages
    71
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Doubs (Franche Comté)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux

    Informations forums :
    Inscription : Août 2017
    Messages : 71
    Points : 25
    Points
    25
    Par défaut WPF - 2ème source de valeur
    Bonjour

    Je remplis ma fenêtre avec une liste de données, je mets les objets dans la liste puis quand terminé je fais DataContext :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Application.Current.Dispatcher.Invoke(new Action(() => this.DataContext = lstOperations));
    Cependant dans ma boucle je fais également une somme d'une quantité que je souhaite afficher dans un label, contrairement à avant ce n'est qu'une somme de toutes les opérations., la valeur n'est plus dans la liste.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="0.03*" />
                <RowDefinition Height="0.04*" />
                <RowDefinition Height="0.04*" />
                <RowDefinition Height="0.88*"/>
                <RowDefinition Height="0.02*" />
            </Grid.RowDefinitions>
            <Label Grid.Row="1" />
            <Button Name="btnParamsAffichage" HorizontalAlignment="Left" Width="100" Grid.Row="1" Background="#b2c9ef" Click="btnParamsAffichage_Click"  />
     
            <Label Grid.Row="2" />
        <ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="3" >
            <ItemsControl x:Name="lvDataBinding" HorizontalContentAlignment="Stretch" BorderThickness="0" Margin="10" Grid.Row="3" Background="{x:Null}" ItemsSource="{Binding}" Foreground="White">
                <ItemsControl.ItemTemplate>
                    <DataTemplate DataType="{x:Type local:CL_Operation}">
                        <Border BorderBrush="White" BorderThickness="1" CornerRadius="3" Margin="0,3" Grid.ColumnSpan="0" Background="Transparent" HorizontalAlignment="Left" VerticalAlignment="Top">
                            <Grid Width="{Binding strWidth}" Height="60" Background="Transparent" Margin="0,1">
                                <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="0.06*" />
                                        <ColumnDefinition Width="0.09*" />
                                        <ColumnDefinition Width="0.1*" />
                                        <ColumnDefinition Width="0.12*" />
                                        <ColumnDefinition Width="0.1*" />
                                        <ColumnDefinition Width="0.1*" />
                                        <ColumnDefinition Width="0.1*" />
                                        <ColumnDefinition Width="0.05*" />
                                        <ColumnDefinition Width="0.05*" />
                                        <ColumnDefinition Width="0.05*" />
                                        <ColumnDefinition Width="0.05*" />
                                        <ColumnDefinition Width="0.03*" />
                                    </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition />
                                    <RowDefinition />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                    <UniformGrid Tag="{Binding strRueck}"  Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" MouseLeftButtonUp="Test" >
                                        <UniformGrid.Style>
                                            <Style TargetType="{x:Type UniformGrid}">
                                                <Style.Triggers>
                                                    <Trigger Property="IsMouseOver" Value="true">
                                                        <Setter Property="Background" Value="White"/>
                                                    </Trigger>
                                                    <Trigger Property="IsMouseOver" Value="false">
                                                        <Setter Property="Background" Value="{Binding scbBackColor}"/>
                                                    </Trigger>
                                                </Style.Triggers>
                                            </Style>
                                        </UniformGrid.Style>
                                        <TextBlock Text="{Binding strPriorite}" FontSize="36" Foreground="Red" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold"/>
                                </UniformGrid>
                                    <UniformGrid  Grid.Column="0" Grid.Row="2"  Background="{Binding scbBackColor}" MouseLeftButtonUp="Test" >
                                        <TextBlock Text="{Binding strSsSect}" FontSize="14" Foreground="Black" HorizontalAlignment="Right" VerticalAlignment="Bottom" FontWeight="Bold" />
                                    </UniformGrid>
                                    <UniformGrid  Grid.Column="1" Grid.Row="0" Grid.RowSpan="3">
                                    <TextBlock Text="{Binding strRetardAvanceText}" FontSize="28" Foreground="{Binding scbColorRetardAvance}" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" />
                                </UniformGrid>
                                <UniformGrid  Grid.Column="2" Grid.Row="0" Grid.ColumnSpan="2" Grid.RowSpan="2">
                                    <UniformGrid.Style>
                                        <Style TargetType="{x:Type UniformGrid}">
                                            <Style.Triggers>
                                                <Trigger Property="IsMouseOver" Value="true">
                                                    <Setter Property="Background" Value="Gray"/>
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </UniformGrid.Style>
                                        <TextBlock Tag="{Binding strRueck}" Foreground="{Binding scbColorNumOfOp}" Text="{Binding strNoOfOp}" FontSize="26" FontWeight="Bold" MouseLeftButtonUp="Test"/>
                                </UniformGrid>
                                <UniformGrid  Grid.Column="2" Grid.Row="2" Grid.ColumnSpan="3">
                                    <TextBlock Text="{Binding strDescriptionOp}" FontSize="14" Foreground="White"/>
                                </UniformGrid>
                                <UniformGrid Grid.Column="4" Grid.Row="0">
                                    <TextBlock Text="{Binding strNoArticle}" FontSize="14" Foreground="White"/>
                                </UniformGrid>
                                <UniformGrid  Grid.Column="4" Grid.Row="1">
                                    <TextBlock Text="{Binding strDateFormatee}" FontSize="14" Foreground="White"/>
                                </UniformGrid>
                                <UniformGrid Grid.Column="5" Grid.Row="0">
                                    <TextBlock Text="{Binding strPcesPoste}" FontSize="14" Foreground="White"/>
                                </UniformGrid>
                                <UniformGrid  Grid.Column="5" Grid.Row="1">
                                    <TextBlock Text="{Binding strTpsPoste}" FontSize="14" Foreground="White"/>
                                </UniformGrid>
                                <UniformGrid  Grid.Column="6" Grid.Row="0">
                                    <TextBlock Text="{Binding strTempsPoste}" FontSize="14" Foreground="White"/>
                                </UniformGrid>
                                <UniformGrid  Grid.Column="6" Grid.Row="1">
                                    <TextBlock Text="{Binding strClient}" FontSize="14" Foreground="White"/>
                                </UniformGrid>
                                <UniformGrid  Grid.Column="5" Grid.Row="2" Grid.ColumnSpan="3">
                                    <TextBlock Text="{Binding strTypeOp}" FontSize="14" Foreground="White"/>
                                </UniformGrid>
                                    <UniformGrid  Grid.Column="9" Grid.Row="3">
                                        <TextBlock Text="{Binding strAuart}" FontSize="12" Foreground="White" FontWeight="Bold"/>
                                    </UniformGrid>
                                </Grid>
                        </Border>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Columns="2"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </ScrollViewer>
            <Label HorizontalAlignment="Right" Foreground="White" Grid.Row="4" Content="{Binding Path=strTotaux}"></Label>
            <Label HorizontalAlignment="Center" Foreground="White" Grid.Row="4" Content=""></Label>
        </Grid>
    C'est l'avant dernière ligne avec le Binding strTotaux

    J'aimerais remplir une variable strTotaux, qui n'a plus rien à avoir avec ma liste et afficher son contenu

    Dans mon cas j'ai des données qui s'affichent si je fais une variable strTotaux dans ma classe mais du coup il ne m'affiche que la valeur de strTotaux de la classe du 1er enregistrement de la liste donc rien n'est encore additionné.



    Comment procéder ? merci

  2. #2
    Nouveau membre du Club
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Août 2017
    Messages
    71
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Doubs (Franche Comté)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux

    Informations forums :
    Inscription : Août 2017
    Messages : 71
    Points : 25
    Points
    25
    Par défaut
    Bonjour

    Je n'ai toujours malheureusement à ce jour pas de solution à mon problème

    Ai-je été assez précis et est-ce assez clair l'explication de mon problème afin que quelqu'un puisse m'aider ?

    Merci par avance et salutations

  3. #3
    Membre chevronné
    Homme Profil pro
    edi
    Inscrit en
    Juin 2007
    Messages
    905
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : edi

    Informations forums :
    Inscription : Juin 2007
    Messages : 905
    Points : 1 923
    Points
    1 923
    Par défaut
    Si ta liste d'opération est le DataContext de ta fenêtre tu ne peux pas utiliser de propriétés autres que celles de la liste. Il faut que tu crées un nouveau ViewModel qui aura en propriétés exposées la liste d'opération, le total calculé et tout ce dont tu pourras avoir besoin. Et dans le chemin de Binding des contrôles de ta fenêtre il te faudra rajouter le nom de la propriété pour faire le lien.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
     
    public class MainWindowViewModel :INotifyPropertyChanged
    {
      private ObservableCollection<LS_Operation> _operations;
      public ObservableCollection<LS_Operation> Operations
      {
        get { return _operation; }
        set
        {
          if(value == _operations) return;
          _operations = value;
          OnPropertyChanged("Operations"); 
        }
      }
     
      private string _strTotal;
      public string StrTotal
      {
        get { return _strTotal; }
        set
        {
          if(value == _strTotal) return;
          _strTotal = value;
          OnPropertyChanged("StrTotal");
        }
      }
     
      public event PropertyChangedEventHandler PropertyChanged;
      protected void OnPropertyChanged(string propertyName)
      {
        var handler = PropertyChanged;
        if (handler != null)
        {
          handler(this, new PropertyChangedEventArgs(propertyName));
        }
      }
    }
    Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
    <ItemsControl x:Name="lvDataBinding" HorizontalContentAlignment="Stretch" BorderThickness="0" Margin="10" Grid.Row="3" Background="{x:Null}" ItemsSource="{Binding Operations}" Foreground="White">

    Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
    <Label HorizontalAlignment="Right" Foreground="White" Grid.Row="4" Content="{Binding Path=StrTotaux}"></Label>

  4. #4
    Nouveau membre du Club
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Août 2017
    Messages
    71
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Doubs (Franche Comté)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux

    Informations forums :
    Inscription : Août 2017
    Messages : 71
    Points : 25
    Points
    25
    Par défaut
    Salut Noxen

    Merci beaucoup pour ta réponse

    Cependant je ne comprends pas :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     private ObservableCollection<LS_Operation> _operations;
                public ObservableCollection<LS_Operation> Operations
                {
                    get { return _operation; }
                    set
                    {
                        if (value == _operations) return;
                        _operations = value;
                        OnPropertyChanged("Operations");
                    }
                }
    Pourquoi ce bout de code ?

    Je déclare déjà en haut ma liste :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     public ObservableCollection<CL_Operation> lstOperationsTemp = new ObservableCollection<CL_Operation>();
    De plus, tu nommes la variable strTotal ou _strTotal et dans le Binding on envoie strTotaux ? je suis un peu perdu. Pour peut-être un peu clarifier tout ça :

    Mon code XAML :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:Plutus" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="Plutus.MainWindow"
            Title="MainWindow" Height="523.725" Width="898" Background="#FF3A3939" WindowStartupLocation="CenterScreen" WindowState="Maximized">
        <Window.Resources>
            <Style TargetType="ScrollBar">
                <Setter Property="Width" Value="35"/>
            </Style>
        </Window.Resources>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="0.03*" />
                <RowDefinition Height="0.04*" />
                <RowDefinition Height="0.04*" />
                <RowDefinition Height="0.88*"/>
                <RowDefinition Height="0.02*" />
            </Grid.RowDefinitions>
            <Label Grid.Row="1" />
            <Button Name="btnParamsAffichage" Content="Paramètres" HorizontalAlignment="Left" Width="100" Grid.Row="1" Background="#b2c9ef" Click="btnParamsAffichage_Click"  />
     
            <Label Grid.Row="2" />
        <ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="3" >
            <ItemsControl VerticalAlignment="Top" x:Name="lvDataBinding" HorizontalContentAlignment="Stretch" BorderThickness="0" Margin="10" Grid.Row="3" Background="{x:Null}" ItemsSource="{Binding}" Foreground="White">
                <ItemsControl.ItemTemplate>
                    <DataTemplate DataType="{x:Type local:CL_Operation}">
                        <Border BorderBrush="White" BorderThickness="1" CornerRadius="3" Margin="0,3" Grid.ColumnSpan="0" Background="Transparent" HorizontalAlignment="Left" VerticalAlignment="Top">
                            <Grid Width="{Binding strWidth}" Height="60" Background="Transparent" Margin="0,1">
                                <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="0.06*" />
                                        <ColumnDefinition Width="0.09*" />
                                        <ColumnDefinition Width="0.1*" />
                                        <ColumnDefinition Width="0.12*" />
                                        <ColumnDefinition Width="0.1*" />
                                        <ColumnDefinition Width="0.1*" />
                                        <ColumnDefinition Width="0.1*" />
                                        <ColumnDefinition Width="0.05*" />
                                        <ColumnDefinition Width="0.05*" />
                                        <ColumnDefinition Width="0.05*" />
                                        <ColumnDefinition Width="0.05*" />
                                        <ColumnDefinition Width="0.03*" />
                                    </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition />
                                    <RowDefinition />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                    <UniformGrid Tag="{Binding strRueck}"  Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" MouseLeftButtonUp="Test" >
                                        <UniformGrid.Style>
                                            <Style TargetType="{x:Type UniformGrid}">
                                                <Style.Triggers>
                                                    <Trigger Property="IsMouseOver" Value="true">
                                                        <Setter Property="Background" Value="White"/>
                                                    </Trigger>
                                                    <Trigger Property="IsMouseOver" Value="false">
                                                        <Setter Property="Background" Value="{Binding scbBackColor}"/>
                                                    </Trigger>
                                                </Style.Triggers>
                                            </Style>
                                        </UniformGrid.Style>
                                        <TextBlock Text="{Binding strPriorite}" FontSize="36" Foreground="Red" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold"/>
                                </UniformGrid>
                                    <UniformGrid  Grid.Column="0" Grid.Row="2"  Background="{Binding scbBackColor}" MouseLeftButtonUp="Test" >
                                        <TextBlock Text="{Binding strSsSect}" FontSize="14" Foreground="Black" HorizontalAlignment="Right" VerticalAlignment="Bottom" FontWeight="Bold" />
                                    </UniformGrid>
                                    <UniformGrid  Grid.Column="1" Grid.Row="0" Grid.RowSpan="3">
                                    <TextBlock Text="{Binding strRetardAvanceText}" FontSize="28" Foreground="{Binding scbColorRetardAvance}" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" />
                                </UniformGrid>
                                <UniformGrid  Grid.Column="2" Grid.Row="0" Grid.ColumnSpan="2" Grid.RowSpan="2">
                                    <UniformGrid.Style>
                                        <Style TargetType="{x:Type UniformGrid}">
                                            <Style.Triggers>
                                                <Trigger Property="IsMouseOver" Value="true">
                                                    <Setter Property="Background" Value="Gray"/>
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </UniformGrid.Style>
                                        <TextBlock Tag="{Binding strRueck}" Foreground="{Binding scbColorNumOfOp}" Text="{Binding strNoOfOp}" FontSize="26" FontWeight="Bold" MouseLeftButtonUp="Test"/>
                                </UniformGrid>
                                <UniformGrid  Grid.Column="2" Grid.Row="2" Grid.ColumnSpan="3">
                                    <TextBlock Text="{Binding strDescriptionOp}" FontSize="14" Foreground="White"/>
                                </UniformGrid>
                                <UniformGrid Grid.Column="4" Grid.Row="0">
                                    <TextBlock Text="{Binding strNoArticle}" FontSize="14" Foreground="White"/>
                                </UniformGrid>
                                <UniformGrid  Grid.Column="4" Grid.Row="1">
                                    <TextBlock Text="{Binding strDateFormatee}" FontSize="14" Foreground="White"/>
                                </UniformGrid>
                                <UniformGrid Grid.Column="5" Grid.Row="0">
                                    <TextBlock Text="{Binding strPcesPoste}" FontSize="14" Foreground="White"/>
                                </UniformGrid>
                                <UniformGrid  Grid.Column="5" Grid.Row="1">
                                    <TextBlock Text="{Binding strTpsPoste}" FontSize="14" Foreground="White"/>
                                </UniformGrid>
                                <UniformGrid  Grid.Column="6" Grid.Row="0">
                                    <TextBlock Text="{Binding strTempsPoste}" FontSize="14" Foreground="White"/>
                                </UniformGrid>
                                <UniformGrid  Grid.Column="6" Grid.Row="1">
                                    <TextBlock Text="{Binding strClient}" FontSize="14" Foreground="White"/>
                                </UniformGrid>
                                <UniformGrid  Grid.Column="5" Grid.Row="2" Grid.ColumnSpan="3">
                                    <TextBlock Text="{Binding strTypeOp}" FontSize="14" Foreground="White"/>
                                </UniformGrid>
                                    <UniformGrid  Grid.Column="9" Grid.Row="3">
                                        <TextBlock Text="{Binding strAuart}" FontSize="12" Foreground="White" FontWeight="Bold"/>
                                    </UniformGrid>
                                </Grid>
                        </Border>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Columns="2"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </ScrollViewer>
            <Label HorizontalAlignment="Right" Foreground="White" Grid.Row="4" Content="{Binding Path=StrTotal}"></Label>
            <Label HorizontalAlignment="Center" Foreground="White" Grid.Row="4" Content=""></Label>
        </Grid>
     
     
     
    </Window>
    Mon code C# : (j'ai mis en commentaire dans la boucle ou je fais les actions)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    public partial class MainWindow : Window
        {
            public ObservableCollection<CL_Operation> lstOperationsTemp = new ObservableCollection<CL_Operation>();
     
            public List<string> lstSelectedLignesChoisi = new List<string>();
            public List<string> lstSelectedIlotsChoisi = new List<string>();
            public List<string> lstPostesTravailChoisi = new List<string>();
     
     
            DispatcherTimer dtTimer = new DispatcherTimer();
     
            DataTable dtOps = new DataTable();
            DataView dvOps = new DataView();
     
            Thread thrGetList;
     
            string connectionString = "Data Source=SERVER\\INSTANCE;Initial Catalog=DB;Integrated Security=True";
            string strOrderBy;
            string strResultTri;
            string strWhere;
            string strWhereLigne;
            string strWhereIlot;
            string strWherePosteTravail;
     
            int iTimer = 30;
     
            public MainWindow()
            {
                InitializeComponent();
     
                dtTimer.Tick += new EventHandler(timer_Tick);
                dtTimer.Interval = new TimeSpan(0, 0, 1);
                //dtTimer.Start();
     
                thrGetList = new Thread(GetList);
                thrGetList.Start();
            }
     
            public CL_Operation clOp { get; set; }
     
            public void GetList()
            {
                dvOps = dtOps.DefaultView;
     
                Application.Current.Dispatcher.Invoke(new Action(() => lstOperationsTemp.Clear()));
                dtOps.Clear();
     
                string strQueryDatas;
     
                SqlConnection con = new SqlConnection(connectionString);
     
                strQueryDatas = "SELECT * FROM [DB].[dbo].[VIEW] " + strWhere + " " + strOrderBy;
     
                SqlDataAdapter adOps = new SqlDataAdapter(strQueryDatas, con);
     
                adOps.Dispose();
     
                try
                {
                    con.Open();
                    SqlCommand sqlCmd = new SqlCommand(strQueryDatas, con);
                    sqlCmd.CommandTimeout = 500;
                    adOps.SelectCommand = sqlCmd;
                    adOps.Fill(dtOps);
                    adOps.Dispose();
                    sqlCmd.Dispose();
                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
     
                DateTime dtNow = DateTime.Now;
     
                double dblTotalQte = 0;
                double dblTotalOp = 0;
                double dblTotalTemps=0;
     
                for (int i = 0; i < dvOps.Count; i++)
                {
                    clOp = new CL_Operation();
     
                    clOp.strWidth = Convert.ToString(Convert.ToInt32(System.Windows.SystemParameters.PrimaryScreenWidth) / 2);
     
                    clOp.strNoOfOp = dvOps[i]["NumOF"].ToString().Trim() + " - " + dvOps[i]["NumOP"].ToString().Trim();
                    clOp.strAuart = dvOps[i]["AUART"].ToString().Trim();
                    clOp.strMaufnr = dvOps[i]["MAUFNR"].ToString().Trim();
                    clOp.strCodeLigne = dvOps[i]["CodeLigne"].ToString().Trim();
                    //clOp.strNumOp = dvOps[i]["NumOP"].ToString().Trim();
                    clOp.strRueck = dvOps[i]["RUECK"].ToString().Trim();
                    clOp.strDescriptionOp = dvOps[i]["DesOpe"].ToString().Trim();
                    clOp.strNoArticle = dvOps[i]["NumArt"].ToString().Trim();
                    clOp.strDateFormatee = dvOps[i]["DateFormatee"].ToString().Trim();
                    clOp.strPcesPoste = Convert.ToString(Convert.ToDouble(String.Format("{0:0}", dvOps[i]["QtePoste"].ToString().Trim()))) + " pces";
                    clOp.strTpsPoste = Convert.ToString(Convert.ToDouble(String.Format("{0:0.00}", dvOps[i]["TpsChargePoste"].ToString()))) + " Hrs";
                    clOp.strClient = dvOps[i]["NomClientCourt"].ToString().Trim();
                    clOp.strTypeOp = dvOps[i]["MAKTX"].ToString();
                    clOp.strPriorite = dvOps[i]["PriorOF"].ToString();
                    clOp.strSsSect = dvOps[i]["SsSect"].ToString();
                    clOp.strDateDerBadge = Convert.ToDateTime(dvOps[i]["DatDerBadg"]);
     
                    // C'est ici que je calcule les totaux
                    dblTotalQte += Convert.ToDouble(dvOps[i]["QtePoste"].ToString().Trim());
                    dblTotalTemps += Convert.ToDouble(dvOps[i]["TpsChargePoste"].ToString().Trim());
                    dblTotalOp++;
     
                    // Que je veux passer dans une variable ici pour afficher dans le label en bas de mon XAML
                    clOp.strTotaux = "Nb OPs : " + dblTotalOp.ToString() + " Qte totale : " + dblTotalQte.ToString() + " Pces Temps total : " + dblTotalTemps + " Hrs";
     
                    Application.Current.Dispatcher.Invoke(new Action(() => lstOperationsTemp.Add(clOp)));
     
                    DateTime dtRetard = Convert.ToDateTime(clOp.strDateFormatee);
                    DateTime dtTempsPoste = Convert.ToDateTime(clOp.strDateDerBadge);
                    TimeSpan tRetard = dtNow.Date - dtRetard.Date;
                    TimeSpan tTempsPoste = dtNow - dtTempsPoste;
     
                    clOp.strRetardAvanceText = Convert.ToString(Math.Abs(Convert.ToDouble(tRetard.TotalDays))) + " jrs";
     
                    clOp.scbBackColor.Freeze();
                    clOp.scbColorRetardAvance.Freeze();
     
                }
     
                ObservableCollection<CL_Operation> lstOperations = new ObservableCollection<CL_Operation>(lstOperationsTemp);
     
                Application.Current.Dispatcher.Invoke(new Action(() => this.DataContext = lstOperations));
     
            }
     
            private void Test(object sender, MouseButtonEventArgs e)
            {
                string str = ((TextBlock)sender).Tag.ToString();
     
                MessageBox.Show(str);
     
                foreach (var item in lstOperationsTemp)
                {
                    if (item.strRueck == str)
                    {
                        MessageBox.Show(item.strNoOfOp);
                        break;
                    }
                }
            }
     
            public class MainWindowViewModel : INotifyPropertyChanged
            {
                //private ObservableCollection<LS_Operation> _operations;
                //public ObservableCollection<LS_Operation> Operations
                //{
                //    get { return _operation; }
                //    set
                //    {
                //        if (value == _operations) return;
                //        _operations = value;
                //        OnPropertyChanged("Operations");
                //    }
                //}
     
                public string _strTotal;
                public string StrTotal
                {
                    get { return _strTotal; }
                    set
                    {
                        if (value == _strTotal) return;
                        _strTotal = value;
                        OnPropertyChanged("StrTotal");
                    }
                }
     
                public event PropertyChangedEventHandler PropertyChanged;
                protected void OnPropertyChanged(string propertyName)
                {
                    var handler = PropertyChanged;
                    if (handler != null)
                    {
                        handler(this, new PropertyChangedEventArgs(propertyName));
                    }
                }
            }
        }
    Ma classe (ClassStructure.cs)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    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;
    using System.Data;
    using System.Data.SqlClient;
     
    namespace Project
    {
        public class CL_Operation
        {
            public string strWidth { get; set; }
            public string strAuart { get; set; }
            public string strMaufnr { get; set; }
            public string strNoOfOp { get; set; }
            public string strRueck { get; set; }
            public string strCodeLigne { get; set; }
            public string strNumOp { get; set; }
            public string strDescriptionOp { get; set; }
            public string strNoArticle { get; set; }
            public string strDateFormatee { get; set; }
            public string strPcesPoste { get; set; }
            public string strTpsPoste { get; set; }
            public string strClient { get; set; }
            public string strTypeOp { get; set; }
            public string strPriorite { get; set; }
            public string strPoste { get; set; }
            public string strSsSect { get; set; }
            public string strTotaux { get; set; }
            public DateTime strDateDerBadge { get; set; }
            public string strTempsPoste { get; set; }
            public string strRetardAvanceText { get; set; }
            public SolidColorBrush scbColorNumOfOp { get; set; }
            public SolidColorBrush scbColorRetardAvance { get; set; }
            public SolidColorBrush scbBackColor { get; set; }
        }
     
    }
    C'est dans cette classe comme tu peux voir que j'ai mis une public string strTotaux.

    La variable se remplie bien avec mon code C# ci-dessus dans la boucle cependant pour chaque objet dans ma liste la valeur est différente et il m'affiche la valeur de la 1ère, donc tout à 1 ... rien additionné.

    Merci

  5. #5
    Membre chevronné
    Homme Profil pro
    edi
    Inscrit en
    Juin 2007
    Messages
    905
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : edi

    Informations forums :
    Inscription : Juin 2007
    Messages : 905
    Points : 1 923
    Points
    1 923
    Par défaut
    Citation Envoyé par Fellerson Voir le message
    De plus, tu nommes la variable strTotal ou _strTotal et dans le Binding on envoie strTotaux ? je suis un peu perdu.
    C'est simplement une erreur de typo, dans le binding ça aurait du être StrTotal.

    Il y a quelque chose que je ne comprends pas très bien sur ta propriété strTotaux. Si c'est un membre spécifique de CL_Operation alors tu dois l'afficher dans le DataTemplate de ton ItemsControl. Si c'est une somme totale sur l'ensemble de tes CL_Operation alors :
    - ce n'est pas une prorpiété de CL_Operation mais de l'objet qui manipule ta liste de CL_Operation (ici ta fenêtre) -> il faut la sortir de ta classe ;
    - le DataContext de ta fenêtre est ta liste d'opération, ce qui l'empêche d'accéder à cette propriété ; on en revient au cas précédent, le DataContext de ta fenêtre ne doit pas être ta liste de CL_Operation mais ta fenêtre elle-même, ce qui signifie qu'il te faudra aussi mettre à jour le Binding pour l'ItemsSource de ton ItemsControl.

    Je te conseille également de te renseigner sur le pattern MVVM (Model-View-ViewModel) : tutoriel

  6. #6
    Nouveau membre du Club
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Août 2017
    Messages
    71
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Doubs (Franche Comté)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux

    Informations forums :
    Inscription : Août 2017
    Messages : 71
    Points : 25
    Points
    25
    Par défaut
    Citation Envoyé par Noxen Voir le message
    C'est simplement une erreur de typo, dans le binding ça aurait du être StrTotal.

    Il y a quelque chose que je ne comprends pas très bien sur ta propriété strTotaux. Si c'est un membre spécifique de CL_Operation alors tu dois l'afficher dans le DataTemplate de ton ItemsControl. Si c'est une somme totale sur l'ensemble de tes CL_Operation alors :
    - ce n'est pas une prorpiété de CL_Operation mais de l'objet qui manipule ta liste de CL_Operation (ici ta fenêtre) -> il faut la sortir de ta classe ;
    - le DataContext de ta fenêtre est ta liste d'opération, ce qui l'empêche d'accéder à cette propriété ; on en revient au cas précédent, le DataContext de ta fenêtre ne doit pas être ta liste de CL_Operation mais ta fenêtre elle-même, ce qui signifie qu'il te faudra aussi mettre à jour le Binding pour l'ItemsSource de ton ItemsControl.

    Je te conseille également de te renseigner sur le pattern MVVM (Model-View-ViewModel) : tutoriel
    Salut.

    Ok, c'est pas trop clair pour moi donc je veux bien un peu plus d'aide.

    Oui, c'est une somme totale de mes CL_Operation

    Comment la sortir de ma classe ?

    Le datacontext est ma liste oui.

    Tout n'est pas encore très clair pour moi. Je vais lire ton tuto mais si tu peux m'aider un peu plus je suis preneur.

    Merci

Discussions similaires

  1. Récupérer la 2ème plus grande valeur dans une variable
    Par lyonnaisdu26 dans le forum QlikView
    Réponses: 3
    Dernier message: 12/03/2012, 13h58
  2. Une visite guidée de WPF - 2ème partie (Disposition)
    Par tomlev dans le forum Windows Presentation Foundation
    Réponses: 0
    Dernier message: 11/11/2010, 02h35
  3. [WPF] <image source="" />
    Par Gregory.M dans le forum Windows Presentation Foundation
    Réponses: 14
    Dernier message: 04/03/2010, 16h01
  4. [WPF] ListView ajout de valeurs
    Par NeoKript dans le forum C#
    Réponses: 0
    Dernier message: 28/11/2009, 21h22
  5. Tri d'une zone de liste avec origine source : liste valeurs?
    Par electrosat03 dans le forum VBA Access
    Réponses: 1
    Dernier message: 12/05/2009, 21h01

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo