How to do relativesource mode find ancestor (or equivalent) in UWP

前端 未结 2 1626
名媛妹妹
名媛妹妹 2020-11-28 13:02

I am trying to do something that one would think should be very simple (at least it is in WPF). I have a page with a listbox and a datatemplate, now that datatemplate calls

2条回答
  •  日久生厌
    2020-11-28 13:44

    There is a concept called x:Bind in Windows 10 UWP. In x:Bind the code behind becomes the datacontext for the binding. So if you add a property in the user control's code behind, pointing to the view model, that can be used to bind the command.

    public class MyButton
    {
       public ViewModel ButtonViewModel 
       { 
          get
          { 
              return ButtonViewModelObject;
          }
       }
    }    
    

    In XAML -

    Refer - https://msdn.microsoft.com/en-us/library/windows/apps/mt204783.aspx

                                     OR
    

    You can use ElementName with traditional binding to achieve the result.

    Refer - Can't access datacontext of parent

    Update: To access delete command from the page's datacontext, the following method can be used, assuming - the change of usercontrol's datacontext (from customer) to the page's datacontext doesn't affect anything else present inside the usercontrol.

    
            
     
    
    

提交回复
热议问题