4
votes

I have a user control written in C# & WPF using the MVVM pattern.

All I want to do is have a property in the bound ViewModel exposed to outside of the control. I want to be able to bind to it and I want any changes to the property to be picked up by anything outside the control that is bound to the exposed value.

This sounds simple, but its making me pull out my hair (and there is not much of that left).

I have a dependency property in the user control. The ViewModel has the property implementing the INotifyPropertyChanged interface and is calling the PropertyChanged event correctly.

Some questions: 1) How do I pick up the changes to the ViewModel Property and tie it to the Dependency Property without breaking the MVVM separation? So far the only way I've managed to do this is to assign the ViewModels PropertyChanged Event in the Controls code behind, which is definitely not MVVM.

2) Using the above fudge, I can get the Dependency property to kick off its PropertyChangedCallback, but anything bound to it outside the control does not pick up the change.

There has to be a simple way to do all of this. Note that I've not posted any code here - I'm hoping not to influence the answers with my existing code. Also, you'd probably all laugh at it anyway...

Rob

OK, to clarify - code examples:

usercontrol code behind:

   public static DependencyProperty NewRepositoryRunProperty = DependencyProperty.Register("NewRepositoryRun", typeof(int?), typeof(GroupTree),
                                                                new FrameworkPropertyMetadata( null, new PropertyChangedCallback(OnNewRepositoryRunChanged)));
    public int? NewRepositoryRun
    {
        get { return (int?)GetValue(NewRepositoryRunProperty); }
        set
        {
            SetValue(NewRepositoryRunProperty, value);
        }
    }

    private static void OnNewRepositoryRunChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.OldValue != e.NewValue)
        {

        }
    }

    public GroupTree()
    {
        InitializeComponent();

        GroupTreeVM vm = new GroupTreeVM();

        this.DataContext = vm;

    }

Viewmodel (GroupTreeVM.cs)

   private int? _NewRepositoryRun;
    public int? NewRepositoryRun
    {
        get
        {
            return _NewRepositoryRun;
        }
        set
        {
            _NewRepositoryRun = value; 
            NotifyPropertyChanged();
        }
    }
2
Please post your (relevant) code.dymanoid
Hair loss question probably belong to some other site - do we have Beauty.SE? Maybe your CRT monitor is killing you. Please switch to paper. Alternatively edit out non-programming related text from your post ...Alexei Levenkov
I have no idea what you're talking about. WPF does not support hair, or lack of it, and I don't see any XAML or C# code in your post. Close-voting. -1Federico Berasategui
@Corcus oh, ffs, that one. Here's a hint: (this.Content as FrameworkElement).DataContext = this is about the dumbest thing you can do. Simply use Binding.ElementName to reference properties defined on the root.user1228
Okay, keep hitting yourself in the head with a hammer. It's your head, after all. Also, POOF, no more peen. Enjoy sitting down to pee.user1228

2 Answers

12
votes

And now for my weekly "don't do that" answer...

Creating a ViewModel for your UserControl is a code smell.

You're experiencing this issue because of that smell, and it should be an indication that you're doing something wrong.

The solution is to ditch the VM built for the UserControl. If it contains business logic, it should be moved to an appropriate location in another ViewModel.

You should think of a UserControl as nothing more than a more complex control. Does the TextBox have its own ViewModel? No. You bind your VM's property to the Text property of the control, and the control shows your text in its UI.

Think of UserControls in MVVM like this--For each model, you have a UserControl, and it is designed to present the data in that model to the user. You can use it anywhere you want to show the user that model. Does it need a button? Expose an ICommand property on your UserControl and let your business logic bind to it. Does your business logic need to know something going on inside? Add a routed event.

Normally, in WPF, if you find yourself asking why it hurts to do something, it's because you shouldn't do it.

0
votes

Perhaps I've misunderstood, but it seems like you're trying to use binding in the code behind?

public MyUserControl()
{
    InitializeComponent();

    // Set your datacontext.

    var binding = new Binding("SomeVMProperty");
    binding.Source = this.DataContext;

    SetBinding(MyDependencyProperty, binding);
}