I'm building my first MVVM Application in WPF. On this moment I'm trying to raise an event by double clicking on a item in a listbox. This event is handled by the View as shown below in the code.
Now I want to send the ViewModel the index of the item in the listbox which is doubleclicked. How do i do that?
PS: the ClassObject and the ClassDiagram are both custom classes, they have both the same attribute "Name"
View
public partial class ProjectView : UserControl
{
public ProjectView()
{
InitializeComponent();
this.DataContext = new ProjectViewModel();
}
public void listBoxProject_MouseDoubleClick(object sender,MouseButtonEventArgs e)
{
MessageBox.Show(listBoxProject.SelectedIndex.ToString()); //Send index to ViewModel
}
}
XAML
<ListBox x:Name="listBoxProject" ItemsSource="{Binding CollectionList}" HorizontalAlignment="Stretch" Margin="-1,32,-1,-1" VerticalAlignment="Stretch" Width="auto" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
DisplayMemberPath="Name"
SelectedValuePath="Name"
>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<EventSetter Event="MouseDoubleClick" Handler="listBoxProject_MouseDoubleClick"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
ViewModel
namespace VITUMLEditor.ViewModels
{
public class ProjectViewModel:BaseViewModel
{
private readonly CompositeCollection collectionList = new CompositeCollection();
public ICommand AddClassCommand
{
get { return new DelegateCommand(AddClass); }
}
public ICommand AddClassDiagramCommand
{
get { return new DelegateCommand(AddClassDiagram); }
}
private void AddClass()
{
collectionList.Add(new ClassObject("Class" + ClassObject.Key, VisibilityEnum.Public));
}
private void AddClassDiagram()
{
collectionList.Add(new ClassDiagram("ClassDiagram" + ClassDiagram.Key));
}
public CompositeCollection CollectionList
{
get { return collectionList; }
}
}
}