0

I am still a little new to wpf and MVVM. I am trying to code a solution without breaking that pattern. I have two (well three, but for the scope of this question just two) DataGrids. I want to double click on the row of one, and from that load data Into the second DataGrid (ideally I would spin up a second thread that would load the data). So far I can get a window to pop up when I double click on a row. I throw the code for the event into the code behind for the xaml. To me that seems very windows formish. Somehow or the other I feel like that breaks the pattern a great deal.

private void DataGrid_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e) {
    if (popDataGrid.SelectedItem == null) {
        return;
    }
    var selectedPopulation = popDataGrid.SelectedItem as PopulationModel;
    MessageBox.Show(string.Format("The Population you double clicked on has this ID - {0}, Name - {1}, and Description {2}", selectedPopulation.populationID, selectedPopulation.PopName, selectedPopulation.description));
}

That is the code for the event in the code behind and here is the grids definition in the xaml:

<DataGrid ItemsSource="{Binding PopulationCollection}" Name="popDataGrid"
          AutoGenerateColumns="False" RowDetailsVisibilityMode="VisibleWhenSelected"
          CanUserAddRows="False" Margin="296,120,0,587" HorizontalAlignment="Left" Width="503" Grid.Column="1" 
          MouseDoubleClick="DataGrid_MouseDoubleClick">
</DataGrid>

I am thinking this code should go in the MainWindowViewModel. So I am attempting to create a command:

public ICommand DoubleClickPopRow { get { return new DelegateCommand(OnDoubleClickPopRow); }}

and the same event handler:

private void OnDoubleClickPopRow(object sender, MouseButtonEventArgs e) {
}

But the ICommand is throwing an exception when it returns the DelegateCommand(OnDoubleClickPopRow).

Well, one can plainly see that the number of arguments doesn't match. I know I am doing something wrong, but I am not quite sure what it is. I will continue to research this but any help you guys can give would be greatly appreciated.

1 Answer 1

1
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<DataGrid ItemsSource="{Binding PopulationCollection}" Name="popDataGrid"
AutoGenerateColumns="False" RowDetailsVisibilityMode="VisibleWhenSelected"
CanUserAddRows="False" Margin="296,120,0,587" HorizontalAlignment="Left" Width="503"  Grid.Column="1" SelectedItem="{Binding ItemInViewModel}"></DataGrid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding Save_Bid}" />
</i:EventTrigger>
</i:Interaction.Triggers>

You can add this to your DataGrid and add your code in your viewmodel. Now that we have a selected item bound to an item in our view model we can use that item to know when we can fire the even we want as well as what item to use when the event is fired When the event can be fired

bool Can_Fire_Event()
{
if(ItemInViewModel != null)
{ return true; } else { return false; }
}
private RelayCommand _saveBid;
public ICommand SaveBid
{
get
{
if (_saveBid == null)
{
_saveBid = new RelayCommand(param => Save_Bid(), param => Can_Fire_Event());
}
return _saveBid;
}
}

public void Save_Bid()
{
//Open your new Window here, using your "ItemInViewModel" because this event couldn't be fired from your datagrid unless the "ItemInViewModel" had a value assigned to it

}
Sign up to request clarification or add additional context in comments.

8 Comments

Where would I add this code? I tried adding it to my before My DataGridTextColumns rows. What about that names space? I am getting an exception telling me it can't find it? Is there a dll I need to download?
I am still confused on how to implement this. Could you suggest some reading?
microsoft.com/downloads/en/… Download the SDK which is free
I was loading the wrong dll... But I still don't understand per-se how to capture the event and lets say display a window with a message and all the items from the row of that data element?
So your command that your firing would go to a relay command in your view model. From your view model you would also have a selected item bound to your grid. So when double click happens, in your view model you would know what item has been double clicked and you could open a new window from there as well as set the data context for that new window.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.