1

I know, title is a little confusing so let me explain. I have a user control that has a dependency property. I access this dependency property with a regular property called Input. In my view model I also have a property called Input. I have these two properties bound together in XAML using two-way binding as shown below:

<uc:rdtDisplay x:Name="rdtDisplay" Input="{Binding Input, Mode=TwoWay}" Line1="{Binding myRdt.Line1}" Line2="{Binding myRdt.Line2}" Height="175" Width="99"  Canvas.Left="627" Canvas.Top="10"/>

Okay in my view model, I call a method whenever the value of Input is changed as shown in my property:

public string Input
        {
            get
            {
                return input;
            }
            set
            {
                input = value;
                InputChanged();
            }
        }

The problem with this is that when I set the value of Input in my view model it only updates the value of the variable input as per my setter in my property. How can I get this to update back to the dependency property in the user control? If I leave the code input = value; out then I get a compilation error.

I need something like this:

public string Input
            {
                get
                {
                    return UserControl.Input;
                }
                set
                {
                    UserControl.Input = value;
                    InputChanged();
                }
            }

If I make the Input property in my view model look like this:

public string Input
        {
            get; set;
        }

then it works, however, I am unable to call the InputChanged() method that I need to call when the Property is changed. All suggestions are appreciated.

3
  • The Input property should be bound to the DependencyProperty. It appears you're doing this, but I can't understand why it isn't working. You wouldn't happen to have built a ViewModel for your UserControl, did you? Like creating a rdtDisplayViewModel that you use inside of the UserControl to perform all your UI work? Something tells me you've gone and done this....
    – user1228
    Dec 22, 2015 at 21:59
  • I have some code in the code behind of my user control, but not a separate view model. Dec 22, 2015 at 22:02
  • 1
    For two way to work, your CLR Input property should use some change notification.
    – AnjumSKhan
    Dec 23, 2015 at 4:18

1 Answer 1

1

Implement INotifyPropertyChanged in your ViewModel

public class Sample : INotifyPropertyChanged
{
    private string input = string.Empty;
    public string Input
    {
        get
        {
            return input;
        }
        set
        {
            input = value;
            NotifyPropertyChanged("Input");
            InputChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

}

In your case, you can do it in the code behind of your usercontrol

5
  • 2
    No, DO NOT DO THIS in "code behind of usercontrol"! Do this in your viewmodel. To be refactory safe inherit from Microsoft.Practices.Prism.ViewModel.NotificationObject and in the setter after InputChanged(); add RaisePropertyChanged(() => Input);
    – gReX
    Dec 23, 2015 at 10:31
  • 1
    please edit your answer by removing the last sentence regarding the code behind, see my comment.
    – gReX
    Dec 23, 2015 at 12:10
  • 1
    It is not at all mandatory that he should create a viewmodel. Since he has already mentioned that he doesn't have a viewmodel, I suggested this. If he was following MVVM, what you suggested was right.
    – Subru
    Dec 23, 2015 at 12:21
  • I disagree. It ist clear, that he is following the mvvm Pattern, because the question is tagged as MVVM and ProgrammingDude wrote: 'in my view model, I call a method'
    – gReX
    Dec 23, 2015 at 13:13
  • Your answer is correct and helped me out, but gRex was right. I had to do it in my viewModel so that the value that I set in my viewModel was reflected back to my user control. Dec 23, 2015 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.