Reputation: 2153
So I enabled a rightclick option for my DataGrid. I want to display just one property of the selecteditem but it's not behaving like how I would like. It displays my namespace and extra.
public class Paymentinfo
{
public int PaymentNo { get; set; }
public String Date { get; set; }
public double Payment { get; set; }
public double Principle { get; set; }
public double Interest { get; set; }
public double Balance { get; set; }
}
private void MenuItem_OnClick(object sender, RoutedEventArgs e)
{
MessageBox.Show(AmortGrid.SelectedItem.ToString());
}
I am trying to implement this without using a viewmodel! If I put a breakpoint where Messagebox is and put the cursor over the selectedItem then it'll display the properties paymentNo-date-payment-principle-interest-balance. The only value I require is PaymentNo
was hoping it'd be something like this
MessageBox.Show(AmortGrid.SelectedItem.PaymentNo.ToString());
Upvotes: 1
Views: 45
Reputation: 3861
Create a ToString() method on PaymentInfo.
public class Paymentinfo
{
public override string ToString()
{
return PaymentNo.ToString();
}
}
Upvotes: 2
Reputation: 374
You can also set the SelectedValuePath and instead of using SelectedItem use SelectedValue.
Upvotes: 2
Reputation: 66459
When you call ToString()
like that, you get the name of the class type, which is what you're seeing.
If that's a collection of Paymentinfo
, cast SelectedItem
back to that type first:
MessageBox.Show(((Paymentinfo)AmortGrid.SelectedItem).PaymentNo.ToString());
FWIW, I'd reconsider the ViewModel
. Far easier to test your code if you get it out of the code-behind.
You'd be able to bind your SelectedItem
directly to a property in the ViewModel
(perhaps called SelectedPaymentinfo), and then there'd be no messing around with casting.
Upvotes: 3