Partage
  • Partager sur Facebook
  • Partager sur Twitter

WPF - Retourner la valeur d'un ItemControl

    14 septembre 2017 à 8:20:50

    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 :
    <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 :
    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
    • Partager sur Facebook
    • Partager sur Twitter
      14 septembre 2017 à 12:03:24

      Une solution simple mais qui marche. À adapter à ton cas (j'ai testé ça fonctionne)

      https://stackoverflow.com/questions/2006507/add-parameter-to-button-click-event

      • Partager sur Facebook
      • Partager sur Twitter
        14 septembre 2017 à 12:26:39

        Salut Stit84

        merci énormément pour ta réponse

        C'est ce que je cherchais. Sauf que je cherchais avec "Value" ou "Item"... Effectivement avec TAG ça fonctionne mieux. Merci encore une fois pour ton aide .



        -
        Edité par Fellerson 15 septembre 2017 à 15:43:42

        • Partager sur Facebook
        • Partager sur Twitter
          15 septembre 2017 à 15:43:46

          Bonjour

          Savez-vous comment récupérer la valeur d'un UnifromGrid en sender ?

          <UniformGrid Tag="{Binding strRueck}"  Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Background="{Binding scbBackColor}" MouseLeftButtonUp="Test" >

          En tout logique 

          string str = ((TextBlock)sender).Tag.ToString();

          Ne fonctionne plus car ce n'est plus un TextBlock mais un UniFormGrid. Grid ne fonctionne pas à la place de TextBlock

          Merci

          • Partager sur Facebook
          • Partager sur Twitter

          WPF - Retourner la valeur d'un ItemControl

          × 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