views:

922

answers:

2

Hi all,

I am knocking together a WPF demo for our department at work to show them the advantages of WPF whilst trying to adhere to our development standards (dependency injection and developing objects to an explicit interface).

I have come to a bit of a wall now. I am implementing the View using the MVVM design pattern and I need to update a TextBlocks Text property every time the property on the View Model (VM) is updated. For this I would define the VM property as a Dependency Property and bind the TextBlocks Text property in the View to it.

Now the MV property is on my interface and is (as per our development standards) explicitly defined. From the View I bind the Text property of the TextBlock in the View to the Dependency Properties property (not the static part) but this does not update my View when the dependency properties value changes (I know how to bind to an explicit interface so this is not the problem as far as I can see).

Any help would really be appreciated. Can I use Dependency Properties with Explicit Interfaces? If I can how, if not have you got any ideas on what I can do in this situation?

Thank you for reading and I look forward to your responses.

Adam

+4  A: 

I'm not entirely sure if I understood your question right, but why not simply use INotifyPropertyChanged on your ViewModel?

for example:

interface MyInterface : INotifyPropertyChanged
{
    string Text { get; set; }
}

class MyViewModel : MyInterface
{
    private string text;
    public string Text 
    {
        get { return text; }
        set 
        { 
            if (text != value)
            {
               text = value;
               // TODO: Raise the NotifyPropertyChanged event here
            }
        }
    }
}

With this you should be able to simply bind to the Text property

Mark Heath
Sorry I didnt realise anyone responded to this! I will give it a go and let you know. Thank you for the suggestion!
Yes that worked thank you very much for the suggestion... I have more reading to do! :)
A: 

The question is mildly confusing but I will take a stab at it. I tried several variations of the dependency property and could not make it work with the following interface.

interface IViewModel
{
    string Text { get; set; }
}

I registered the property on the implementing class using the following syntax (each one in a different test).

public static readonly DependencyProperty TextProperty = 
    DependencyProperty.Register("Text", 
        typeof(string), typeof(IViewModel));

Then I tried implementing the interface explicitly or implicitly to no avail. The only combination that did work is when I used.

public static readonly DependencyProperty TextProperty = 
    DependencyProperty.Register("Text", 
        typeof(string), typeof(ImplementingClass));

If you run into problems and are looking for other WPF samples you may want to check out.

Good luck.

smaclell