简体   繁体   中英

Binding to a Property of UserControl

I wrote a widget that contains lots of stuff, and it contains a property (DependencyProperty) that is being updated from inside the UC class (based on input being typed into a textbox and input from button clicks etc..) Its definition is :

public partial class UserControlClassName : UserControl, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

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

    public static readonly DependencyProperty ValueProperty = System.Windows.DependencyProperty.Register("Value", typeof(string), typeof(UserControlClassName), new PropertyMetadata(string.Empty, OnValueChanged));

    public string Value
    {
        get { return (string)GetValue(ValueProperty); }
        set 
        { 
            SetValue(ValueProperty, value);
            NotifyPropertyChanged("Value");
        }
    }

    private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue != null && e.NewValue != e.OldValue)
        {
            Console.Out.WriteLine("new value: " + e.NewValue);
        }
    }

   ...
   ...
   lots of other code that updates the Value property..
   ...
   ...

}

I'm instantiating the UC in some window's XAML as follows:

<GeneralControls:UserControlClassName
            x:Name="someRandomName"
            Value="{Binding MyViewModel.MyBoundedValueField, StringFormat=N2, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DebugBinding}}"
            VerticalAlignment="Bottom" />

And to complete the picture, this is the view model's property i'm using to "grab" the value :

public string MyBoundedValueField
    {
        get { return myBoundedValueField; }
        set
        {
            if (myBoundedValueField!= value)
            {
                myBoundedValueField = value;
                OnPropertyChanged("MyBoundedValueField");
            }
        }
    }

My problem is- the Value property that inside the user control do get updated, but the outer property i'm binding to in the xaml (myBoundedValueField ) is not getting the updates.. something with the binding to this dependency property is not working- so I attached a converter to debug this and the converter does not get called so its definitely a wrong binding setup..

(Tnx to any1 who help!)

The solution eventually was to add Mode=TwoWay

<GeneralControls:UserControlClassName
    x:Name="someRandomName"
    Mode=TwoWay
    Value="{Binding MyViewModel.MyBoundedValueField, StringFormat=N2, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DebugBinding}}"
    VerticalAlignment="Bottom" />

I don't know as to why the binding did not work without it - I would love to hear an explanation if any1 can shade some light on the subject..

Special thanks to @Clemens!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM