XAML: access to controls inside user control

前端 未结 3 796
有刺的猬
有刺的猬 2021-01-14 08:45

I have userControl like this:


    

        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-14 09:48

    To achieve this task, one solution could be to declare a Dependency Property into your Control (which seems to be called "MyButton") code behind :

      public string ButtonText
      {
        get { return (string )this.GetValue(ButtonTextProperty); }
        set { this.SetValue(ButtonTextProperty, value); } 
      }
      public static readonly DependencyProperty ButtonTextProperty = DependencyProperty.Register(
        "ButtonText", typeof(string), typeof(MyButton),new PropertyMetadata(false));
    

    Then you have to bind to it into your xaml code :

      
        ... // some xaml code
        
            
            
        
     
    

    Finally, you are able to use your "MyButton" Control :

    
    
    

提交回复
热议问题