locked
Binding Custom property inside it's custom control RRS feed

  • Question

  • hi all,

    sorry if it is stupid question...I've spent one day in google but can not do it.

    I create one custom control called DisplayData and one custom dependency property call Adress. Inside the Custom control, I want to bind the Adress property to some thing, How can I do that?

    Many thanks.

    Hung Doan 

    Tuesday, April 5, 2011 2:54 AM

Answers

  • Hi,

    following is the reply that i prepared for other post but it binds the Dependency Property to the local property on the XAML as well.

    take a look....

    u can make a Dependency Property in ur User Control like this

     

    public partial class UserControl1 : UserControl
     {
      public UserControl1()
      {
       InitializeComponent();
       
      }
    
      [BindableAttribute(true)]
      public bool MyProperty
      {
       get { return (bool)GetValue(MyPropertyProperty); }
       set { SetValue(MyPropertyProperty, value); }
      }
    
      // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
      public static readonly DependencyProperty MyPropertyProperty =
       DependencyProperty.Register("MyProperty", typeof(bool), typeof(UserControl1), new UIPropertyMetadata(false));

     

    here i've taken a Bool DP..

    u can use it in ur XAML like this and bind it with the mainwindow property, here i am using the UserControl in the MainWindow and IsError is the local property of the MainWindow which is Binded to the Dependency property

     

    <local:UserControl1 MyProperty="{Binding IsError,Mode=TwoWay}"/>
    
    
      private bool isError = true;
    
      public bool IsError
      {
       get { return isError; }
       set { isError = value;
       RaisePropertyChanged("IsError");
       if (IsError == true)
        btn1.IsEnabled = false;
    //Here i set the IsEnable property of the Button
       }
      }
    

     

    so when ever Error comes in ur UserControl u can set the Dependency Property to True which in turn will call the Set method of the IsError property in MainWindow where u can set the IsEnable property of the Button

    i have taken the Button click event in the UserControl to set the DP

     

     private void Button_Click(object sender, RoutedEventArgs e)
      {
       MyProperty = true;
      }

     

    hope it helps u...



    Thanks, BHavik
    • Marked as answer by Sheldon _Xiao Tuesday, April 12, 2011 7:34 AM
    Wednesday, April 6, 2011 5:01 AM
  • thank you every one, i can do it now.
    • Marked as answer by Sheldon _Xiao Tuesday, April 12, 2011 7:34 AM
    Thursday, April 7, 2011 1:35 AM

All replies

  • Hi hundoan,

    Is it possible for you to post your code?

     


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This will help other members to find the solution easily.
    Tuesday, April 5, 2011 3:51 AM
  • Inside my user control call Display, i  create one custom dependency property call Adress

    public static readonly DependencyProperty AdressProperty = 

    DependencyProperty.Register( "Adress", typeof(string),

    typeof(Datadisplay),  new FrameworkPropertyMetadata(

             new PropertyChangedCallback(ChangeData))); 

    So how can i bind it or access it in code or XAML (in Display user control code),

    Sorry if it is stupid, but i can not do it.

    many thanks

     

    Tuesday, April 5, 2011 6:48 AM
  • Hi,

    The following code should work. I think you are missing the public property block - Adress (BOLD). This is accessible in both XAML and code behind.

    public static readonly DependencyProperty AdressProperty =
        DependencyProperty.Register(
        "Adress", typeof(string), typeof(Datadisplay),  new FrameworkPropertyMetadata(new PropertyChangedCallback(ChangeData)));


    public bool Adress
    {
        get { return (string)GetValue(AdressProperty); }
        set { SetValue(AdressProperty, value); }
    }

    Please refer the following link.

    http://msdn.microsoft.com/en-us/library/ms752914.aspx

     


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This will help other members to find the solution easily.
    • Proposed as answer by Sheldon _Xiao Tuesday, April 12, 2011 7:35 AM
    Tuesday, April 5, 2011 7:15 AM
  • You should use RelativeSource:

    {Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type c:MyCustomControl}}, Path=MyDependencyProperty}

    • Proposed as answer by Sheldon _Xiao Tuesday, April 12, 2011 7:35 AM
    Tuesday, April 5, 2011 8:09 AM
  • thank you,

    However in this case, the Custom DependencyProperty act as source in binding. However i want it to be target, because i want to bind it (onewaytosource) with ObjectDataProvider.MethodParameters. How can i do it? 

    Wednesday, April 6, 2011 4:48 AM
  • Hi,

    following is the reply that i prepared for other post but it binds the Dependency Property to the local property on the XAML as well.

    take a look....

    u can make a Dependency Property in ur User Control like this

     

    public partial class UserControl1 : UserControl
     {
      public UserControl1()
      {
       InitializeComponent();
       
      }
    
      [BindableAttribute(true)]
      public bool MyProperty
      {
       get { return (bool)GetValue(MyPropertyProperty); }
       set { SetValue(MyPropertyProperty, value); }
      }
    
      // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
      public static readonly DependencyProperty MyPropertyProperty =
       DependencyProperty.Register("MyProperty", typeof(bool), typeof(UserControl1), new UIPropertyMetadata(false));

     

    here i've taken a Bool DP..

    u can use it in ur XAML like this and bind it with the mainwindow property, here i am using the UserControl in the MainWindow and IsError is the local property of the MainWindow which is Binded to the Dependency property

     

    <local:UserControl1 MyProperty="{Binding IsError,Mode=TwoWay}"/>
    
    
      private bool isError = true;
    
      public bool IsError
      {
       get { return isError; }
       set { isError = value;
       RaisePropertyChanged("IsError");
       if (IsError == true)
        btn1.IsEnabled = false;
    //Here i set the IsEnable property of the Button
       }
      }
    

     

    so when ever Error comes in ur UserControl u can set the Dependency Property to True which in turn will call the Set method of the IsError property in MainWindow where u can set the IsEnable property of the Button

    i have taken the Button click event in the UserControl to set the DP

     

     private void Button_Click(object sender, RoutedEventArgs e)
      {
       MyProperty = true;
      }

     

    hope it helps u...



    Thanks, BHavik
    • Marked as answer by Sheldon _Xiao Tuesday, April 12, 2011 7:34 AM
    Wednesday, April 6, 2011 5:01 AM
  • Hi hungdoan,

    If your issue persists after reading BHavik's solution, could you post more codes or share a simple sample with us, I think it will be very helpful for us to resovle your issue.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 6, 2011 2:59 PM
  • thank you every one, i can do it now.
    • Marked as answer by Sheldon _Xiao Tuesday, April 12, 2011 7:34 AM
    Thursday, April 7, 2011 1:35 AM