Reputation: 860
I'm trying to design a WPF Datagrid, where the RowDetails (a textblock) will bind to the Details
string property of the grid's ItemSource elements.
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock Text="{Binding Details}" .....
</DataTemplate>
</DataGrid.RowDetailsTemplate>
The trick is, it's possible the elements in the binded collection might not have a Details
property, in which case RowDetails should just be empty. Is there a way to have WPF binding to a property that might not even exist at runtime, without throwing an exception? (To be clear, the items in the binded collection will either all have a Details
property, or they all won't. I am not talking about a collection holding multiple object types.)
I suppose one answer is to force the users of this grid to provide objects that always have a Details property (IDetails interface or whatever), but I think it would be nice to not have to do this - I'd like the grid to be very flexible in the objects it can display.
Upvotes: 1
Views: 534
Reputation: 860
After much looking around, I discovered PriorityBindings, which gets the job done. Here I'm binding to the TextBlock inside the RowDetailsTemplate, and set the Fallbackvalue to empty string if the binded property is not found:
<TextBlock.Text>
<PriorityBinding FallbackValue="">
<Binding Path="Details" IsAsync="True"/>
</PriorityBinding>
</TextBlock.Text>
Upvotes: 1