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

前端 未结 2 1629
名媛妹妹
名媛妹妹 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:34

    The answer is Dependency Property. I have had the same issue. First if you have no DataTemplate involved the solution is straight forward:

    (this.Content as FrameworkElement).DataContext = this;
    

    You set the DataContext of the UserControl in its constructor to its code behind.

    If you are planning to us your Command inside a DataTemplate you will need a DependecyProperty too.

    Example:

     
         

    And to back it up you create a dependency property for that command:

     public ICommand MyCommand
        {
            get { return (ICommand)GetValue(MyCommandProperty); }
            set { SetValue(MyCommandProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for MyCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyCommandProperty =
            DependencyProperty.Register("MyCommand", typeof(ICommand), typeof(ownerclass), new PropertyMetadata(0));
    

    So now when you use your user control you will have a MyCommand property on it that you can bind to any command from your ViewModel as long as the templating parent matches the one that you provided and also the parameter is bound to the actual item that the control is part of.

    
    

    Simple example:

    UserControl code behind

     public sealed partial class ListviewUserControl : UserControl
    {
        public ListviewUserControl()
        {
            this.InitializeComponent();
    
            (this.Content as FrameworkElement).DataContext = this;
        }
    
    
    
    
        public ICommand ButtonCommand
        {
            get { return (ICommand)GetValue(ButtonCommandProperty); }
            set { SetValue(ButtonCommandProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for ButtonCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ButtonCommandProperty =
            DependencyProperty.Register("ButtonCommand", typeof(ICommand), typeof(ListviewUserControl), new PropertyMetadata(null));
    
    
    
    
        public ObservableCollection ItemsSource
        {
            get { return (ObservableCollection)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(ObservableCollection), typeof(ListviewUserControl), new PropertyMetadata(new ObservableCollection()));
    
    
    
    }
    

    UserControl Xaml:

    
        
            
                
                    
                    
                
            
        
    
    

    Usage in Page.xaml:

    
    

提交回复
热议问题