Reputation: 8977
SelectedItemProperty
of the datagrid to a property on my ViewModel called CurrentlySelectedPreviousDocument
OpenPreviousDocumentCommand
OpenPreviousDocumentCommand
runs a method called OpenSelectedPreviousDocument()
OpenSelectedPreviousDocument()
uses the CurrentlySelectedPreviousDocument
property to copy a file to the temporary directory and open it.More scenarios to help describe the problem:
XAML DataGrid bindings:
<DataGrid
x:Name="PreviousDocumentsDataGrid"
ItemsSource="{Binding PreviousDocumentsList}"
SelectedItem="{Binding CurrentlySelectedPreviousDocument, Mode=OneWayToSource}"
SelectionMode="Single"
SelectionUnit="FullRow"
AutoGenerateColumns="False"
IsReadOnly="True"
HorizontalGridLinesBrush="LightGray"
VerticalGridLinesBrush="LightGray"
BorderBrush="Transparent"
Visibility="{Binding PreviousDocumentsFound, Converter={StaticResource BoolToVisConverter}}">
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding OpenPreviousDocumentCommand}"/>
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Reference Type" Binding="{Binding ReferenceType}"/>
<DataGridTextColumn Header="Category ID" Binding="{Binding Category}"/>
<DataGridTextColumn Header="Description" Binding="{Binding Description}"/>
<DataGridTextColumn Header="Document Timestamp" Binding="{Binding Timestamp}"/>
</DataGrid.Columns>
</DataGrid>
The ViewModel definition of CurrentlySelectedPreviousDocument:
public VEDocument CurrentlySelectedPreviousDocument
{
get { return _currentlySelectedPreviousDocument; }
set { _currentlySelectedPreviousDocument = value;
OnPropertyChanged("CurrentlySelectedPreviousDocument");} //TODO: Is the on propertychanged actually necessary here?
}
Command Definition:
public ICommand OpenPreviousDocumentCommand
{
get
{
return _openPreviousDocumentCommand ??
(new CommandHandler(OpenSelectedPreviousDocument, _canExecuteCommands));
}
}
Method in the ViewModel to open the document (uses the viewmodel property)
public void OpenSelectedPreviousDocument()
{
var docToOpen = CurrentlySelectedPreviousDocument;
...etc. etc.
}
Upvotes: 2
Views: 1258
Reputation: 8977
A lesson in debugging and posting questions -- this one is entirely on me.
What I did not mention, but should have, is that in offline mode I was using a fake service to return the list of documents.
If I'd mentioned that, I could have pasted the code and folks could have seen that my fake service wasn't returning a location for two of the documents (hence my issue).
I created a piece of debugging code that outputted the locations from the list itself when my viewmodel was opened. Doing this helped me see that it was the list that had the problem, not the binding. My assumption that I got the binding wrong (due to my inexperience with WPF) was actually a false one.
Big Thanks to sexta13 and meilke for helping me rule out other potential issues.
Upvotes: 1