I have a DataTrigger attached to a style for TextBlock, defined as such:
<DataTrigger Binding="{Binding Path=Link, Converter={StaticResource HasContentConverter}}" Value="True">
<Setter Property="TextDecorations" Value="Underline" />
<Setter Property="Cursor" Value="Hand" />
</DataTrigger>
The issue that I’m having is that I have multiple objects that end up using this style, some of which contain a “Link” property, and some of which don’t. Whenever the system encounters an object that doesn’t, it prints this error in the output window:
BindingExpression path error: ‘Link’ property not found on ‘object’ ”DataRowView’ (HashCode=53681904)’. BindingExpression:Path=Link; DataItem=’DataRowView’ (HashCode=53681904); target element is ‘TextBlock’ (Name=”); target property is ‘NoTarget’ (type ‘Object’)
This is expected behaviour, however I’m wondering if there’s a way to tell the processor in XAML to only apply if the “Link” property exists (ie. check for the property before attempting to bind, or some other method that doesn’t print an error). Is this possible?
So my final solution for this was to have a base DataGrid class that implements the style in question, minus the “Link” specific data trigger. Then I had a new DataGrid class that derived from my base class, with code to specifically create the data trigger:
I was able to use this method because the binding object that had the “Link” property was only used in my derived DataGrid class.