Bonjour,

J'ai le problüme suivant: j'ai créer un DataTemplate contenant un listboxitem qui lui meme contien un stackpanel, une image et un textblock. Si je click sur la zone du StackPanel, l'item se sélectonne pas, je dois clicker hors du StackPanel pour que sa fonctionne....

Voici le code:

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
 
<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="324" Width="483">
 
    <Window.Resources>
 
        <DataTemplate x:Key ="lstItem">
            <ListBoxItem Tag="{Binding Path=Tag}" >
                <StackPanel>
                    <Image Source="http://www.iconarchive.com/icons/artua/dragon-soft/Disk-512x512.png" Width="24" />
                    <TextBlock Text="{Binding Path=Content}"/>
                </StackPanel>
            </ListBoxItem>
        </DataTemplate>
 
        <LinearGradientBrush x:Key="ListBoxItemSelectedBkg" EndPoint="0,1" StartPoint="0,0">
            <GradientStop Color="sc#1, 0.7241676, 0.916768551, 1" Offset="0.032051282051282048"/>
            <GradientStop Color="sc#1, 0.5893368, 0.8144646, 0.995921433" Offset="1"/>
        </LinearGradientBrush>
 
        <LinearGradientBrush x:Key="ListBoxItemMouseOverBkg" EndPoint="0,1" StartPoint="0,0">
            <GradientStop Color="sc#1, 0.813324332, 0.9436713, 1" Offset="0.032051282051282048"/>
            <GradientStop Color="sc#1, 0.7551608, 0.8902375, 0.999111533" Offset="1"/>
        </LinearGradientBrush>
 
 
        <Style x:Key="itemsStyle" TargetType="{x:Type ListBoxItem}">
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Margin" Value="1,3,1,2" />
            <Setter Property="Padding" Value="1,3,1,3" />
            <Setter Property="Template">
 
 
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border x:Name="Bd" CornerRadius="4,4,4,4" BorderThickness="1,1,1,1" 
                  Background="Transparent" Padding="{TemplateBinding Padding}" >
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter Property="Background" Value="{DynamicResource ListBoxItemSelectedBkg}" TargetName="Bd"/>                             
                                <Setter Property="BorderBrush" Value="sc#1, 0.193802863, 0.6061246, 0.828075051" TargetName="Bd"/>
                                <Setter Property="HorizontalContentAlignment" Value="0" />
 
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" Value="{DynamicResource ListBoxItemMouseOverBkg}" TargetName="Bd"/>
                                <Setter Property="BorderBrush" Value="sc#1, 0.413786, 0.7415289, 0.917951047" TargetName="Bd"/>
                                <Setter Property="HorizontalContentAlignment" Value="0" />
 
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="true" />
                                    <Condition Property="IsSelected" Value="true" />
                                </MultiTrigger.Conditions>
                                <MultiTrigger.Setters>
                                    <Setter Property="Background" Value="{DynamicResource ListBoxItemSelectedBkg}" TargetName="Bd"/>
                                </MultiTrigger.Setters>
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="false">                              
                                <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>        
    </Window.Resources>
 
      <Grid>
        <ListBox Margin="33,12,83,48" Name="ListBox1"  ItemTemplate="{StaticResource lstItem}" Height="202" ItemContainerStyle="{StaticResource itemsStyle}" />
    </Grid>
</Window>
Merci.