Reputation: 27
I've got a DataGrid that I've setup a doubleclick eventsetter that calls a method. Below is my xaml, following that is my codebehind page. The double click event works, but the return I get is "system.data.datarowview" and I don't know why. I'm trying to get the "vehicleID" value of the row which is its own column that is hidden.
XAML:
<DataGrid Name="OpenVehicles" AutoGenerateColumns="False" IsReadOnly="False" SelectedItem="{Binding vehicleID}" SelectionUnit="FullRow">
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick" Handler="OpenVehicleClick" />
</Style>
</DataGrid.ItemContainerStyle>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding vehicleID}" Width="*" Header="vehicleID" Visibility="Hidden"/>
<DataGridTextColumn Binding="{Binding date, StringFormat=\{0:MMM dd yyyy \}}" Width="*" Header="Date"/>
<DataGridTextColumn Binding="{Binding companyshortname}" Width="*" Header="Customer"/>
<DataGridTextColumn Binding="{Binding subject}" Width="5*" Header="Vehicle Description"/>
<DataGridTextColumn Binding="{Binding FName}" Width="*" Header="Owner"/>
</DataGrid.Columns>
C# Code:
private void OpenVehicleClick(object sender, RoutedEventArgs e)
{
MessageBox.Show(OpenVehicles.CurrentCell.Item.ToString());
}
Any ideas on how to retrieve the column value or any other suggestions are more than welcome. I'm beyond stumped.
Upvotes: 2
Views: 1134
Reputation: 49985
This bit in your XAML: SelectedItem="{Binding vehicleID}"
means that the selected data item will be bound into the property vehicleID
of the DataContext of the grid (which will be inherited from the control/page that it is on).
I notice that you've excluded any mention of a ItemsSource on the grid - the vehicleID
property should be in the same class as the ItemSource property. You need to ensure that vehicleID
is a public property, not a field - you cannot databind to a field. So, here is a couple of options depending on the way you've set things up:
pubic class MyPage
{
public MyPage()
{
InitializeComponent();
this.DataContext = this;
}
private void OpenVehicleClick(object sender, RoutedEventArgs e)
{
MessageBox.Show(VehicleID != null ? VehicleID.WhateverProperty : "Nothing selected");
}
public MyDataObject VehicleID { get; set; }
}
or, if you have a separate viewmodel assigned to the DataContext of the page/control:
private void OpenVehicleClick(object sender, RoutedEventArgs e)
{
var selectedDataItem = ((MyViewModel) DataContext).VehicleID;
MessageBox.Show(selectedDataItem != null ? selectedDataItem.WhateverProperty : "Nothing selected");
}
Upvotes: 1
Reputation: 1510
Currently you're reffering to the grid itself. Your event handles a row so use the event parameters to find what you need in the selected row (investigate them in debugging).
Upvotes: 0
Reputation: 8370
The DataRowView.Row
property will contain the DataRow
that you want. From there you can access the column value using DataRowView.Row["ColumnName"]
indexer
Upvotes: 1