I was trying to bind my DataGrid columns to a list where the item for a column could be retrieved using an indexer. The indexer type is DateTime.
I am creating the DataGrid columns using code and wanted to create a binding to retrieve the value from the list. In XAML the path would be written as:
{ Binding Path=Values[01/01/2011] }
But since I am doing it in code behind I need to define the path using a PropertyPath, like so:
new Binding{
Path = new PropertyPath("Values[01/01/2011]")
}
There is another overload for the constructor that takes a path and an array of parameters. According to the documentation the parameters are used for indexers. But when I write my binding as
new Binding {
Path = new PropertyPath("Values", new DateTime(2011, 01, 01))
}
the binding cannot resolve the path. Fair enough, I'm not stating that it should look for an indexer. But if I write it as:
new Binding{ Path = new PropertyPath("Values[]", new DateTime(2011, 01, 01)) }
then DateTime.MinValue is passed to the indexer.
Can someone explain to me how I use the PathParameters in the constructor and how I can bind to indexers without having to do a ToString on my value in the actual path?
Based on this MSDN article, you'd need to include "(0)" to indicate where the parameter should be placed. So the following should work:
new Binding {
Path = new PropertyPath("Values[(0)]", new DateTime(2011, 01, 01))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With