Bonjour,
j'ai une application WPF MVVM, j'ai une listbox qui contient un contextmenu et un second contextmenu sur les items de la listbox.
Sur le premier ContextMenu je n'ai pas de souci, par contre sur les item de la listbox je n'arrive pas à intercepeter les RelayCommand.
Je precise que la listbox se trouve dans un usercontrol que j'injecte dan le MAinWindow.

le code .cs du userControl:
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
 public ObservableCollection<string> ListBoxSelectedItems
        {
            get { return (ObservableCollection<string>)GetValue(ListBoxSelectedItemsProperty); }
            set
            {
                SetValue(ListBoxSelectedItemsProperty, value);
            }
        }
 
        public static readonly DependencyProperty ListBoxSelectedItemsProperty =
           DependencyProperty.Register("ListBoxSelectedItems", typeof(ObservableCollection<string>), typeof(UserControlV),
               new PropertyMetadata(
                   new ObservableCollection<string>(),
                   (o, e) =>
                   {
                       if (e.NewValue == null)
                           return;
                   }
               )
           );
mon code Xaml du usercontrol :
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
<ListBox x:Name="lbx" Height="auto" Width="auto"
                 Margin="5 10 0 10"
                 IsSynchronizedWithCurrentItem="True"
                 ItemsSource="{Binding Path=ListBoxSelectedItems,
                                       RelativeSource={RelativeSource FindAncestor,
                                                                      AncestorType={x:Type UserControl}}}"
                 SelectedIndex="{Binding Path=ListBoxSelectedIndex,
                                         RelativeSource={RelativeSource FindAncestor,
                                                                        AncestorType={x:Type UserControl}}}"
                 SelectedItem="{Binding Path=ListBoxSelectedItem,
                                        RelativeSource={RelativeSource FindAncestor,
                                                                       AncestorType={x:Type UserControl}}}"
                 SelectionMode="Extended">
            <ListBox.ContextMenu>
                <ContextMenu>
                    <MenuItem Command="{Binding AddItemCmd}" Header="Add" />
                </ContextMenu>
            </ListBox.ContextMenu>
 
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="ContextMenu">
                        <Setter.Value>
                            <ContextMenu>
                                <MenuItem Command="{Binding Path=RenameItemCmd,
                                                            RelativeSource={RelativeSource Self}}"
                                          CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor,
                                                                                                    AncestorType={x:Type ContextMenu}},
                                                                     Path=PlacementTarget.SelectedItem}"
                                          Header="Rename" />
                                <MenuItem Command="{Binding Path=DeleteItemCmd,
                                                            RelativeSource={RelativeSource Self}}"
                                          CommandParameter="{Binding ElementName=lbx,
                                                                     Path=SelectedItem}"
                                          Header="Delete" />
                                <MenuItem Command="{Binding AddItemCmd,
                                                            RelativeSource={RelativeSource FindAncestor,
                                                                                           AncestorType={x:Type ContextMenu}}}"
                                          CommandParameter="{Binding}"
                                          Header="Add" />
                            </ContextMenu>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
mon code .cs du MainViewModel pour les command :
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
 
 
 private ObservableCollection<string> co_listBoxList;
 
        public ObservableCollection<string> ListBoxList
        {
            get
            {
                return co_listBoxList;
            }
            set
            {
                co_listBoxList = value;
                RaisePropertyChanged(() => ListBoxList);
            }
        }
 
 
private RelayCommand _addItemCmd;
 
        public RelayCommand AddItemCmd
        {
            get
            {
                if (_addItemCmd == null)
                    _addItemCmd = new RelayCommand(() => AddItemListbox());
                return _addItemCmd;
            }
        }
 
        public void AddItemListbox()
        {
            ListBoxList.Add("New item");
            RaisePropertyChanged(() => ListBoxList);
        }
 
 private RelayCommand _renameItemCmd;
 
        public RelayCommand RenameItemCmd
        {
            get
            {
                if (_renameItemCmd == null)
                    _renameItemCmd = new RelayCommand(() => RenameItemListbox());
                return _renameItemCmd;
            }
        }
 
        public void RenameItemListbox()
        {
        }
private RelayCommand _deleteItemCmd;
 
        public RelayCommand DeleteItemCmd
        {
            get
            {
                if (_deleteItemCmd == null)
                {
                    _deleteItemCmd = new RelayCommand(() => DeleteItemCmdListbox());
                }
                return _deleteItemCmd;
            }
        }
 
        public void DeleteItemCmdListbox()
        {
            ListBoxList.Remove(SelectedItemListBox);
            RaisePropertyChanged(() => ListBoxList);
        }
mon code Xaml du MainWindow :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
<usfields:UserControlV x:Name="lbx"
                                                ListBoxSelectedIndex="{Binding SelectedIndexListBox,
                                                                               Mode=TwoWay,
                                                                               UpdateSourceTrigger=PropertyChanged}"
                                                ListBoxSelectedItem="{Binding SelectedItemListBox,
                                                                              Mode=TwoWay,
                                                                              UpdateSourceTrigger=PropertyChanged}"
                                                ListBoxSelectedItems="{Binding ListBoxList,
                                                                               Mode=TwoWay,
                                                                               UpdateSourceTrigger=PropertyChanged}" />
Pour le contextmenu parent il n'a pas de souci mais les item de ma listbox les événement ne sont pas interceptés.
Quelqu'un saurait il comment faire?
Pour les autres questions je les posterais au fil du projet .

Merci d'avance !!!!