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 - Retourner la valeur d'un ItemControl


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    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 : 33
    Localisation : France, Doubs (Franche Comté)

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

    Informations forums :
    Inscription : Août 2017
    Messages : 71
    Par défaut WPF - Retourner la valeur d'un ItemControl
    Bonjour

    Dans mon programme j'ai besoin de retourner une valeur d'un ItemControl TextBlock. Dans mon code XAML, j'ai l'événement MouseLeftButtonUp="Test" sur un TextBlock :

    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
    <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>
     
            <ScrollViewer VerticalScrollBarVisibility="Auto" >
     
            <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.05*" />
                                    <ColumnDefinition Width="0.1*" />
                                    <ColumnDefinition Width="0.1*" />
                                    <ColumnDefinition Width="0.2*" />
                                    <ColumnDefinition Width="0.1*" />
                                    <ColumnDefinition Width="0.2*" />
                                    <ColumnDefinition Width="0.1*" />
                                    <ColumnDefinition Width="0.2*" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition />
                                    <RowDefinition />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <WrapPanel  Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" Background="#FFFF8200">
                                    <!-- <TextBlock Text="Customer" Foreground="#33B6EF" FontSize="20" />-->
                                    <TextBlock Text="{Binding strPriorite}" FontSize="48" Foreground="Red" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" />
                                    <TextBlock Text="{Binding strSsSect}" FontSize="10" Foreground="Black"  HorizontalAlignment="Right" VerticalAlignment="Bottom" />
                                </WrapPanel>
                                <WrapPanel  Grid.Column="1" Grid.Row="0" Grid.RowSpan="3">
                                    <!-- <TextBlock Text="Location" Foreground="#33B6EF" FontSize="20" />-->
                                    <TextBlock Text="{Binding strRetardAvanceText}" FontSize="28" Foreground="{Binding scbColorRetardAvance}" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" />
                                </WrapPanel>
                                <WrapPanel Grid.Column="2" Grid.Row="0" Grid.ColumnSpan="2" Grid.RowSpan="2">
                                    <WrapPanel.Style>
                                        <Style TargetType="{x:Type WrapPanel}">
                                        <Style.Triggers>
                                            <Trigger Property="IsMouseOver" Value="true">
                                                <Setter Property="Background" Value="Gray"/>
                                            </Trigger>
                                        </Style.Triggers>
                                        </Style>
                                    </WrapPanel.Style>
                                    <!-- <TextBlock Text="Created On" Foreground="#33B6EF" FontSize="20" />-->
                                    <TextBlock Text="{Binding strNoOfOp}" FontSize="28" Foreground="White" FontWeight="Bold" MouseLeftButtonUp="Test"/>
                                </WrapPanel>
                                <WrapPanel  Grid.Column="2" Grid.Row="2" Grid.ColumnSpan="3">
                                    <!-- <TextBlock Text="Quantity" Foreground="#33B6EF" FontSize="20" /> -->
                                    <TextBlock Text="{Binding strDescriptionOp}" FontSize="14" Foreground="White"/>
                                </WrapPanel>
                                <WrapPanel Grid.Column="4" Grid.Row="0">
                                    <!-- <TextBlock Text="Created On" Foreground="#33B6EF" FontSize="20" />-->
                                    <TextBlock Text="{Binding strNoArticle}" FontSize="14" Foreground="White"/>
                                </WrapPanel>
                                <WrapPanel  Grid.Column="4" Grid.Row="1">
                                    <!-- <TextBlock Text="Quantity" Foreground="#33B6EF" FontSize="20" /> -->
                                    <TextBlock Text="{Binding strDateFormatee}" FontSize="14" Foreground="White"/>
                                </WrapPanel>
                                <WrapPanel Grid.Column="5" Grid.Row="0">
                                    <!-- <TextBlock Text="Created On" Foreground="#33B6EF" FontSize="20" />-->
                                    <TextBlock Text="{Binding strPcesPoste}" FontSize="14" Foreground="White"/>
                                </WrapPanel>
                                <WrapPanel  Grid.Column="5" Grid.Row="1">
                                    <!-- <TextBlock Text="Created On" Foreground="#33B6EF" FontSize="20" />-->
                                    <TextBlock Text="{Binding strTpsPoste}" FontSize="14" Foreground="White"/>
                                </WrapPanel>
                                <WrapPanel  Grid.Column="6" Grid.Row="0">
                                    <!-- <TextBlock Text="Quantity" Foreground="#33B6EF" FontSize="20" /> -->
                                    <TextBlock Text="{Binding strTempsPoste}" FontSize="14" Foreground="White"/>
                                </WrapPanel>
                                <WrapPanel  Grid.Column="6" Grid.Row="1">
                                    <!-- <TextBlock Text="Quantity" Foreground="#33B6EF" FontSize="20" /> -->
                                    <TextBlock Text="{Binding strClient}" FontSize="14" Foreground="White"/>
                                </WrapPanel>
                                <WrapPanel  Grid.Column="6" Grid.Row="2" Grid.ColumnSpan="2">
                                    <!-- <TextBlock Text="Created On" Foreground="#33B6EF" FontSize="20" />-->
     
                                    <!-- <TextBlock Text="Quantity" Foreground="#33B6EF" FontSize="20" /> -->
                                    <TextBlock Text="{Binding strTypeOp}" FontSize="14" Foreground="White"/>
                                </WrapPanel>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </ScrollViewer>
     
     
     
    </Window>
    Ensuite en C# je peux retourner la valeur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    string str = (sender as TextBlock).Text;
    Cependant, si je souhaite retourner la valeur d'un autre TextBlock de mon ItemControl du XAML, comment est-ce possible ?

    OU mieux encore, est-il possible de retourner une valeur correspondant à ma ligne dans mon Binding, rempli avec this.DataContext = lstOperations;

    Merci

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

    Informations professionnelles :
    Activité : edi

    Informations forums :
    Inscription : Juin 2007
    Messages : 941
    Par défaut
    Le DataContext de ton TextBlock devrait être initialisé avec l'item CL_Operation à afficher :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    var description = ((sender as TextBlock)?.DataContext as CL_Operation)?.strDescriptionOp;
    En MVVM les actions de types cliquer pour effectuer une opération sont généralement gérées par des propriétés de dépendance "bindées" sur une propriété ICommand dans le ViewModel. Certains objets graphiques de WPF disposent bien d'une d'une propriété de dépendance pour cela (ex. Button) mais pas tous. Pour associer une ICommand à un événement click d'un contrôle WPF tu peux importer la library MVVMLight de Laurent Bugion et utiliser EventToCommand.

  3. #3
    Membre confirmé
    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 : 33
    Localisation : France, Doubs (Franche Comté)

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

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

    Merci pour ta réponse

    J'ai fait différemment. Ton avis m'intéresse sur ma solution, en tout cas elle fonctionne : dans mon contrôle TextBlock j'ai défini la propriété Tag et un événement Test avec une valeur unique. XAML :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <TextBlock Tag="{Binding strRueck}" Text="{Binding strNoOfOp}" FontSize="28" Foreground="White" FontWeight="Bold" MouseLeftButtonUp="Test"/>
    Ensuite dans le code C# : je récupère dans une variable la valeur du Tag de l'envoyeur (contrôle cliqué). Je peux ensuite facilement parcourir ma liste et récupérer toutes les valeurs que je souhaite dans celle-ci :

    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
     private void Test(object sender, MouseButtonEventArgs e)
            {
                string str = ((TextBlock)sender).Tag.ToString();
     
                MessageBox.Show(str);
     
                foreach (var item in lstOperations)
                {
                    if (item.strRueck == str)
                    {
                        MessageBox.Show(item.strNoOfOp);
                        break;
                    }
                }
            }
    Qu'en penses-tu ?

Discussions similaires

  1. Fonction qui retourne plusieurs valeurs !
    Par casafa dans le forum C++
    Réponses: 20
    Dernier message: 23/04/2014, 16h56
  2. Réponses: 7
    Dernier message: 15/03/2005, 14h44
  3. [PL/SQL] Fonction qui retourne plusieurs valeurs
    Par Loko dans le forum Oracle
    Réponses: 2
    Dernier message: 07/12/2004, 09h43
  4. fonction retournant une valeur
    Par ryan dans le forum ASP
    Réponses: 4
    Dernier message: 06/09/2004, 17h45
  5. Retourner une valeur avec une fonction
    Par stephtbest dans le forum ASP
    Réponses: 4
    Dernier message: 31/10/2003, 16h37

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