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.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
SoftwareSavant
  • 9,467
  • 27
  • 121
  • 195

1 Answers1

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

}
Jonah Kunz
  • 670
  • 8
  • 19
  • 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? – SoftwareSavant Jun 19 '13 at 17:26
  • I am still confused on how to implement this. Could you suggest some reading? – SoftwareSavant Jun 19 '13 at 20:01
  • 1
    http://www.microsoft.com/downloads/en/details.aspx?FamilyID=75e13d71-7c53-4382-9592-6c07c6a00207&displaylang=en Download the SDK which is free – Jonah Kunz Jun 19 '13 at 21:42
  • 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? – SoftwareSavant Jun 20 '13 at 12:07
  • 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. – Jonah Kunz Jun 20 '13 at 14:31
  • Could you give me an example and update your code? For instance, obviously in my view model I have the collection and the instance that I just clicked on, but how would I refer to it? Using a code behind I just used the DataContext and from there I got the specific row that I clicked on... – SoftwareSavant Jun 20 '13 at 14:40
  • Ah, Selected Item is kind of what I am looking for... I will study your code and see what I come up with. So far so good. What is Can_Fire_Event ... Did you mean MouseDoubleClick? – SoftwareSavant Jun 20 '13 at 16:24
  • Can fire event will return false if there is no selected item. If it is false then the event can't fire. If you attach a relay command to a button like this it will auto disable the button for you. – Jonah Kunz Jun 21 '13 at 15:40