How to Access a Button present inside a Custom Control, from the implementing page?

前端 未结 4 2028
感情败类
感情败类 2021-01-23 00:53

I have my generic.xaml containing the following code:



   

          

        
4条回答
  •  一向
    一向 (楼主)
    2021-01-23 01:14

    I have a feeling, that you're trying to mimic EventSetter behaviour. If I'm right, please just take a look on this simple example:

        
    

    This code assigns your custom event to some text block's regular action directly from XAML (you don't have to pollute your code behind with accessing controls' properties).

    I hope this is helpful, but if not, please give me a shout.

    Edit:

    Sorry for not being perfectly clear (this was just a quickly pasted code snippet). Please have a look on a complete example:

    Styles for your next/previous buttons:

    
    
    
    

    Code behind:

     public event EventHandler MovedPrevious;
     public event EventHandler MovedNext;
    
         protected void OnMovedPrevious(object sender, RoutedEventArgs e)
         {
           if (MovedPrevious != null)
           {
              MovedPrevious(this, e);   
           }
         }
    
         protected void OnMovedNext(object sender, RoutedEventArgs e)
         {
            if (MovedNext != null)
            {
               MovedNext(this, e);   
            }
         }
    

    Since now on you can access OnMovedNext and OnMovedPrevious directly from your control's handling conrol/whatever just as Anthony posted.

    Sorry if my previous answer was confusing, but it supposed to be just an inspiration what to do :)

    Edit:

    I haven't noticed that this regards only Silverlight for which I apologize :) But, it works perfectly for WPF if you wish to try.

提交回复
热议问题