Dave
Dave

Reputation: 462

WPF/Silverlight DataBinding to interface property (with hidden implementation)

I have the following (C#) code

namespace A
{
  public interface IX { bool Prop { get; set; } }
  class X : IX { public bool Prop { ... } } // hidden implementation of IX
}

namespace B
{
  ..
  A.IX x = ...;
  object.DataContext = x;
  object.SetBinding(SomeDependencyProperty, new Binding("Prop"));
  ..
}

So I have a hidden implementation of an interface with a property "Prop" and I need to bind to this property in code.

My problem is that the binding isn't working unless I make class X public.

Is there any way around this problem?

Upvotes: 1

Views: 433

Answers (2)

Dr. Kurt R. Matis
Dr. Kurt R. Matis

Reputation: 1

There is a very good reason for making classes implementing public interfaces internal. This enforces interface-based programming. .Net is all about interfaces, not classes. Rule of thumb: Never expose an concrete, non-primitive type when one can expose an interface.

There is a binding syntax that allows one to get the XAML compiler to cast an object to an interface. It's been mentioned in another post:

{Binding Path=(mynamespacealias:IMyInterface.MyValue)}

Upvotes: 0

ligaz
ligaz

Reputation: 2121

I believe you are doing this in Silverlight, because binding to not public members is not possible there. However it is in WPF.

So for Silverlight you should make your class public.

Upvotes: 1

Related Questions