Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have MVVM application, my first MVVM app. So, I need to make a contextMenu in ListBox. I tried this:

In xaml code
C#
<ListBox HorizontalContentAlignment="Stretch" 
                 ItemTemplate="{DynamicResource UserListBoxItemTemplate}" 
                 ItemsSource="{Binding}" Margin="0,71,0,0" Name="lstAllUsers" 
                 SelectionMode="Extended" Grid.ColumnSpan="3"
                >               
                <ListBox.ContextMenu>
                    <ContextMenu>
                    <MenuItem Command="{Binding RemoveUserCommand}" Header="Remove user" />
                </ListBox.ContextMenu>
            </ListBox>

And in cs:


private DelegateCommand removeUser;
        public ICommand RemoveUserCommand
        {
            get
            {
                if (removeUser == null)
                {
                    removeUser = new DelegateCommand(RemoveUser);
                }
                return removeUser;
            }
        }
        private void RemoveUser()
        {
            int nSelected = this.UserControlList.lstAllUsers.SelectedIndex;
            ((User)this.UserControlList.lstAllUsers.SelectedItem).Status = 2;
            if (nSelected - 1 >= 0)
                this.UserControlList.lstAllUsers.SelectedIndex = nSelected - 1;
            else if (nSelected - 1 <= 0 && this.UserControlList.lstAllUsers.Items.Count != 0)
                this.UserControlList.lstAllUsers.SelectedIndex = nSelected;

            this.UserControlList.lstAllUsers.Items.Refresh();
        }

But it doesn't work.
Posted
Updated 1-May-12 1:27am
v6

Hello
Use this:
HTML
<ListBox.ItemContainerStyle> 
        <Style TargetType="ListBoxItem"> 
            <Setter Property="ContextMenu"> 
                <Setter.Value> 
                    <ContextMenu> 
                       <MenuItem Comma...
                    </ContextMenu> 
                </Setter.Value> 
            </Setter> 
        </Style> 
    </ListBox.ItemContainerStyle> 


Instead of this:
HTML
<ListBox.ContextMenu>
                    <ContextMenu>
                    <MenuItem Command="{Binding RemoveUserCommand}" Header="Remove user" />
                </ListBox.ContextMenu>


Thanks to This
 
Share this answer
 
v3
What you are trying to do here is a weird mix of MVVM and code-behind. Rather than attempting to use the user control to change the status of the item, what you should be doing is changing the status of the item in the data.

Now, assuming you are using the PRISM implementation of DelegateCommand, your RemoveUser method should actually look like this (I'm assuming here that the User info is stored in a class called User in this example):
C#
private void RemoveUser(object listParameter)
{
  if (listParameter == null)
  {
    // The user didn't select a list item.
  }
  User user = listParameter as User;
  if (user != null)
  {
    user.Status = 2;
  }
}
And that's it. As long as you are bound to the data via the DataContext, you should be good to go.
 
Share this answer
 
hi...

Try this..

 public DelegateCommand<object> RemoveUserCommand{ get; set; }


RemoveUserCommand= new DelegateCommand(this.RemoveUser, this.RemoveUserExecute);

  private void RemoveUser()
        {
//do struff
}


public bool RemoveUserExecute()
        {
            return true;
        }

Hope this help Thnaks..
 
Share this answer
 
v3
Hi,

Try this

https://github.com/mlaflamm/listbox-contextmenu

Hope this help Thanks..
 
Share this answer
 
v3
Thx.
But, as for Solution 1, with my sc it doesn't work.

As for Solution 2, I didn't undestand about object listParameter and what to do with it.
 
Share this answer
 
Thank you, but I set breakpoint and it doesn't go to RemoveUserCommand. maybe something wrong with xaml. I used Solution 1 too.
 
Share this answer
 
Comments
Fardan 1-May-12 15:56pm    
not here

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900