Reputation: 1691
I have a UserControl which is just a simple DataGrid to show some stuff, it has his own ViewModel and Model.
I would like to whenever I click on the DataGrid, raise an Event. So far, it's working with commands, and I get the event on my UserControl.ViewModel.
The thing is that I need the event to be raise in the MainWindow.ViewModel, which is who contains the logic to load the stuff needed in the UserControl.DataGrid.
In my little understanding in WPF and MVVM, I have the following options:
This is how I call my UserControl:
<Window>
<Grid>
<TabControl>
<TabItem Header="{Binding Model.TabImportHeader}">
<views:ResultView DataContext="{Binding ResultViewModel}"/>
</TabItem>
</TabControl>
</Grid>
</Window>
And this is my UserControl:
<UserControl x:Class="ResultView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewModels="clr-namespace:ViewModels"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=viewModels:ResultViewModel, IsDesignTimeCreatable=False}">
<!--DataGrid-->
<DataGrid ItemsSource="{Binding Model.ObservableCollection}"
ColumnWidth="*"
AutoGenerateColumns="false"
IsReadOnly="True"
BorderThickness="0"
Background="White"
HeadersVisibility="Column">
<!--InputBindings-->
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftClick"
Command="{Binding LeftClickCommand}">
</MouseBinding>
</DataGrid.InputBindings>
<!--InputBindings-->
<DataGrid.Columns>
<DataGridTextColumn Header="Filename"
Binding="{Binding FileInfo.Name}"
Width="Auto" />
</DataGrid.Columns>
</DataGrid>
</UserControl>
Any WPF Master who also knows a little about good practices? I will really appreciate any help.
Thanks in advance.
EDIT: Command declaration:
public ICommand = new RelayCommand(LeftClick, () => true);
Upvotes: 2
Views: 1845