I have the following situation:
I have a few ViewModel objects, some of which implement an interface ISomeInterface
, some don't. The interfaces exposes a property called SomeEnumeration
(IEnumerable<T>
).
For example:
public sealed class ViewModelA : ViewModelBase, ISomeInterface
{
// ...
IEnumerable<Foo> ISomeInterface.SomeEnumeration
{
get { ...; }
}
}
public sealed class ViewModelB : ViewModelBase
{
// ...
}
My XAML, so far, has been designed in a way that both of the ViewModels happen to have the properties I am binding against (i.e. PropertyA
, PropertyB
, etc.). I haven't ran into the situation yet where a property I am binding against does not exist on the ViewModels that I am setting as the DataContext
. But, now I will... and it will be against a property that is explicitly implemented (I'm not sure if that makes any difference in the WPF Binding Engine).
Basically, my xaml will look like the following:
<StackPanel
Visiblity="{Binding Path=SomeEnumeration, Converter={StaticResource AnyConverter}">
...
</StackPanel>
I'm not sure if this will even work because:
- Not every
DataContext
will contain the property (in case it doesn't, it should be hidden) ... What should I do in this case? - For the
DataContext
s that do contain the property, it is explicitly implemented ... do you have to cast first or something?
Generally, when you want to use the WPF DataBinding Engine, you'll want to also utilize the
FallbackValue
and theTargetNullValue
binding properties. What do these exactly do?Jon explains the binding engine pretty well in this answer:
As far as binding to "explicitly implemented interface", the real question should be how do you set the path to an interface property, because how that interface is implemented does not matter. This is actually quite easy to do in XAML, and here is an example:
So, to answer your questions directly:
FallbackValue
(and optionallyTargetNullValue
if necessary). For example, pass in null when the binding value can not be resolved due to a binding error.XAML usage:
One final note: If the binding fails early, the null
FallbackValue
would not be the value passed into the converter, it would be the final value used whether the binding fails at the property level or the converter level or etc. So do not expect that the converter will still run while passing in null into it.A quick and good fix for you situation would be to place all your logic in the converter which is already in place .
xaml : (your binding)
cs : (your Converter)