Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Custom user control binding

Custom user control binding

Scheduled Pinned Locked Moved WPF
helpwpfcsharphtmlcss
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joe Rozario
    wrote on last edited by
    #1

    Dear friends i have an issue .. i am new to WPF, my requirement is to (binding "custom property" for user control) can some one point out my mistake? User Control ------------- UserControl Code behind -------------------------- public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1), new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))); int nType =0; public int CoType { get {return nType;} set { nType =value; textBlock1.Text = value.ToString(); } } static void textChangedCallBack(DependencyObject property,DependencyPropertyChangedEventArgs args) { UserControl1 searchTextBox = (UserControl1)property; searchTextBox.CoType = (int)args.NewValue; } } Main Form ----------- Main Form Code behind is just InitializeComponent() ViewModel -------------- public int CoTypeValue { get { return nV; } set { if (value != nV) { nV = value; OnPropertyChanged("CType"); } } } The Error message i recive Cannot create instance of 'UserControl1' defined in assembly 'WpfModelViewApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error at object 'System.Windows.Controls.Grid' in markup file If i remove the Dependency property .. the error is not thrown .. but i want to binding for custom property.. and i not sure about the way i have implemented binding for custom property is the right way.. any help????

    P S 2 Replies Last reply
    0
    • J Joe Rozario

      Dear friends i have an issue .. i am new to WPF, my requirement is to (binding "custom property" for user control) can some one point out my mistake? User Control ------------- UserControl Code behind -------------------------- public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1), new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))); int nType =0; public int CoType { get {return nType;} set { nType =value; textBlock1.Text = value.ToString(); } } static void textChangedCallBack(DependencyObject property,DependencyPropertyChangedEventArgs args) { UserControl1 searchTextBox = (UserControl1)property; searchTextBox.CoType = (int)args.NewValue; } } Main Form ----------- Main Form Code behind is just InitializeComponent() ViewModel -------------- public int CoTypeValue { get { return nV; } set { if (value != nV) { nV = value; OnPropertyChanged("CType"); } } } The Error message i recive Cannot create instance of 'UserControl1' defined in assembly 'WpfModelViewApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error at object 'System.Windows.Controls.Grid' in markup file If i remove the Dependency property .. the error is not thrown .. but i want to binding for custom property.. and i not sure about the way i have implemented binding for custom property is the right way.. any help????

      P Offline
      P Offline
      Parwej Ahamad
      wrote on last edited by
      #2

      I have doubt with line:

      public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1),
      new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack)));

      As per my knowledge UIPropertyMetadata is mainly used for provide animation behavioure. I would suggest try with FrameworkPropertyMetaData for databinding purpose. Just try on your side if still it is not working then I will look into that. http://msdn.microsoft.com/en-us/library/ms751554.aspx[^] Thanks for your patience!

      Parwej Ahamad ahamad.parwej@gmail.com

      modified on Tuesday, March 30, 2010 9:53 AM

      J 1 Reply Last reply
      0
      • P Parwej Ahamad

        I have doubt with line:

        public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1),
        new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack)));

        As per my knowledge UIPropertyMetadata is mainly used for provide animation behavioure. I would suggest try with FrameworkPropertyMetaData for databinding purpose. Just try on your side if still it is not working then I will look into that. http://msdn.microsoft.com/en-us/library/ms751554.aspx[^] Thanks for your patience!

        Parwej Ahamad ahamad.parwej@gmail.com

        modified on Tuesday, March 30, 2010 9:53 AM

        J Offline
        J Offline
        Joe Rozario
        wrote on last edited by
        #3

        Dear friend Thanks for your reply , i have changed those lines as public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1), new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))); but no use, if you have already done some samples on Custom Property ..please send it to i.joerozario@gmail.com other wise i will send my sample source.. if u correct that would be great help for me.. Thank you

        P 3 Replies Last reply
        0
        • J Joe Rozario

          Dear friends i have an issue .. i am new to WPF, my requirement is to (binding "custom property" for user control) can some one point out my mistake? User Control ------------- UserControl Code behind -------------------------- public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1), new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))); int nType =0; public int CoType { get {return nType;} set { nType =value; textBlock1.Text = value.ToString(); } } static void textChangedCallBack(DependencyObject property,DependencyPropertyChangedEventArgs args) { UserControl1 searchTextBox = (UserControl1)property; searchTextBox.CoType = (int)args.NewValue; } } Main Form ----------- Main Form Code behind is just InitializeComponent() ViewModel -------------- public int CoTypeValue { get { return nV; } set { if (value != nV) { nV = value; OnPropertyChanged("CType"); } } } The Error message i recive Cannot create instance of 'UserControl1' defined in assembly 'WpfModelViewApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error at object 'System.Windows.Controls.Grid' in markup file If i remove the Dependency property .. the error is not thrown .. but i want to binding for custom property.. and i not sure about the way i have implemented binding for custom property is the right way.. any help????

          S Offline
          S Offline
          Super Lloyd
          wrote on last edited by
          #4

          I don't know exactly why you got this error message, but your "dependency property" is NOT a properly declared and used dependency property! use the "propdp" snippet in VS to declare one

          A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

          1 Reply Last reply
          0
          • J Joe Rozario

            Dear friend Thanks for your reply , i have changed those lines as public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1), new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))); but no use, if you have already done some samples on Custom Property ..please send it to i.joerozario@gmail.com other wise i will send my sample source.. if u correct that would be great help for me.. Thank you

            P Offline
            P Offline
            Parwej Ahamad
            wrote on last edited by
            #5

            Please send me your sample code on ahamad.parwej@gmail.com.

            Parwej Ahamad ahamad.parwej@gmail.com

            1 Reply Last reply
            0
            • J Joe Rozario

              Dear friend Thanks for your reply , i have changed those lines as public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1), new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))); but no use, if you have already done some samples on Custom Property ..please send it to i.joerozario@gmail.com other wise i will send my sample source.. if u correct that would be great help for me.. Thank you

              P Offline
              P Offline
              Parwej Ahamad
              wrote on last edited by
              #6

              Hi Joe, I catch one more thing here you are not using Setter and Getter method:

              public int CoType
              {
              get {return nType;}
              set {
              nType =value;
              textBlock1.Text = value.ToString();
              }
              }

              Should be like:

              public int CoType
              {
              get { return (int)GetValue(coProperty ); }
              set { SetValue(coProperty , value); }
              }

              Change your code and let me know.

              Parwej Ahamad ahamad.parwej@gmail.com

              1 Reply Last reply
              0
              • J Joe Rozario

                Dear friend Thanks for your reply , i have changed those lines as public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1), new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))); but no use, if you have already done some samples on Custom Property ..please send it to i.joerozario@gmail.com other wise i will send my sample source.. if u correct that would be great help for me.. Thank you

                P Offline
                P Offline
                Parwej Ahamad
                wrote on last edited by
                #7

                Basic sample sent on your personal ID.

                Parwej Ahamad ahamad.parwej@gmail.com

                J 1 Reply Last reply
                0
                • P Parwej Ahamad

                  Basic sample sent on your personal ID.

                  Parwej Ahamad ahamad.parwej@gmail.com

                  J Offline
                  J Offline
                  Joe Rozario
                  wrote on last edited by
                  #8

                  Dear Ahamad Thanks for everything.. i found my mistake. in the Usercontrol1.xaml i did not bind the text like Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:UserControl1}}, Path=CoType}" Thank you by Joe

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups