0

I have Textbox and my User Control, how can i Binding Textbox property Text to my Property Message in UserControl?

XAML:

<TextBox Name="txtMessages"/>

<myControl:MyAppBar x:Name="appBar" Message="{Binding ElementName=txtMessages,  Path=Text, Mode=TwoWay}"  Grid.Row="3"/>

And my Property:

     public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
 private string message;
        public string Message
        {
            get
            {
                return message;
            }
            set
            {
                message = value;
                RaisePropertyChanged("Message");
            }
        }

But it's don't work for me. I get error as:

The text associated with this error code could not be found.

Also i try this: i try tihs:

public static readonly DependencyProperty UserControlTextProperty = DependencyProperty.Register(
        "Message",
        typeof(int),
        typeof(ImageList),
        new PropertyMetadata(0, new PropertyChangedCallback(MyControl.MyAppBar.OnUserControlTextPropertyChanged))
        );

 public string Message
        {
            get { return (string)GetValue(UserControlTextProperty); }
            set { SetValue(UserControlTextProperty, value); }
        }

but unsver: "The text associated with this error code could not be found."

goodniko
  • 163
  • 3
  • 14
  • 1
    You have to register a `DependencyProperty` for `Message` in your UserControl: https://msdn.microsoft.com/library/system.windows.dependencyproperty(v=vs.110).aspx – Flat Eric Jan 24 '16 at 13:50
  • @FlatEric update question – goodniko Jan 24 '16 at 14:00
  • 1
    You are registering it for owner type `ImageList` but your control seems to be of type `MyAppBar`. And the dependency property type is `int`, while the CLR property type is `string` – Flat Eric Jan 24 '16 at 14:06
  • @FlatEric thanks so match it's work!;) Another question, if not difficult. what is the difference INotifyPropertyChanged and DepedencyProperty? – goodniko Jan 24 '16 at 14:12
  • 1
    There are several answers to that question, e.g.: http://stackoverflow.com/questions/3551204/when-to-use-a-wpf-dependency-property-versus-inotifypropertychanged – Flat Eric Jan 24 '16 at 14:14

1 Answers1

1

First of all, defining your own DependencyProperty requires keeping naming rule. In the above code "UserControlTextProperty" is not recognized as property name should be "Property" + Property such as "MessageProperty".

public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(int), typeof(ImageList), new PropertyMetadata(0, new PropertyChangedCallback(MyControl.MyAppBar.OnUserControlTextPropertyChanged)));

public string Message
{
    get { return (string)GetValue(MessageProperty); }
    set { SetValue(MessageProperty, value); }
}

It wlll be recognized as a regular dependency property. If you are writing in your own user control, you don't have to separate its ViewModel, but have your control implement INotifyPropertyChanged interface and bind the control's DataContext to itself.

Jay Kim
  • 63
  • 3