如何做相对资源模式查找祖先(或同等)在UWP



我正在尝试做一些人们认为应该非常简单的事情(至少在WPF中是这样)。我有一个带有列表框和数据模板的页面,现在数据模板调用了一个带有按钮的用户控件。没什么特别的,但是按钮命令不是listboxsource的一部分,而且我找不到一种简单的方法来告诉按钮在哪里查找命令。下面是场景

<Page x:Class="App1.MainPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="using:App1">
<Page.Resources>
    <DataTemplate x:Key="MyDataTemplate">
        <local:MyButton />
    </DataTemplate>
</Page.Resources>
<ListBox ItemTemplate="{StaticResource MyDataTemplate}" ItemsSource="{Binding Customers}" />
</Page>
<UserControl x:Class="App1.MyButton"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl, AncestorLevel=2}, Path=DataContext.DeleteCommand}" Content="Delete" />
</UserControl>

请注意这不能编译,因为在UWP中没有模式查找祖先?我该怎么做呢?我一直在谷歌上找,但找不到任何关于它的信息。

谢谢

答案是依赖属性。我也遇到过同样的问题。首先,如果没有涉及到DataTemplate,解决方案是直接的:

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

将UserControl的构造函数中的DataContext设置为其后面的代码。

如果你打算在DataTemplate中使用Command,你也需要一个dependdecyproperty。

的例子:

 <DataTemplate>
     <Button Command="{Binding DataContext.MyCommand, ElementName=ParentName}">
 </DataTemplate>

为了备份它,你为该命令创建了一个依赖属性:

 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));

所以现在当你使用你的用户控件时,你会有一个MyCommand属性,你可以绑定到你的ViewModel中的任何命令,只要模板父类匹配你提供的并且参数被绑定到控件的实际项目的一部分。

<usercontrols:button MyCommand="{Binding MyCommandFromViewModel}" CommandParameter="{Binding}"/>

简单的例子:

UserControl代码

 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<Item> ItemsSource
    {
        get { return (ObservableCollection<Item>)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<Item>), typeof(ListviewUserControl), new PropertyMetadata(new ObservableCollection<Item>()));

}

用户控件Xaml:

<Grid>
    <ListView ItemsSource="{Binding ItemSource}" x:Name="ListView">
        <ListView.ItemTemplate>
            <DataTemplate>
                <!--some item related content-->
                <AppBarButton Icon="Delete" Command="{Binding ButtonCommand, ElementName=ListView}" CommandParameter="{Binding}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

在Page.xaml:

中的用法
<Controls:ListviewUserControl ItemsSource="{Binding ViewModelsItemsList}" ButtonCommand="{Binding ViewModelsCommand}"/>

在Windows 10 UWP中有一个叫做x:Bind的概念。在x:Bind中,后面的代码成为绑定的数据上下文。因此,如果你在用户控件的代码后面添加一个属性,指向视图模型,可以用来绑定命令。

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

In XAML -

<Button Command="{x:Bind ButtonViewModel.DeleteCommand}" Content="Delete" />

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

                                 OR

您可以使用带有传统绑定的ElementName来实现结果。

<Button Command="{Binding DataContext.DeleteCommand, ElementName= UserControlName}" Content="Delete" />

reference - Can't access datacontext of parent

Update:要从页面的数据上下文访问删除命令,可以使用以下方法,假设用户控件的数据上下文(从customer)更改为页面的数据上下文不会影响用户控件中存在的任何其他内容。

<DataTemplate x:Key="MyDataTemplate">
        <local:MyButton DataContext="{Binding DataContext, ElementName = PageName}" />
 </DataTemplate>
<Button Command="{Binding DeleteCommand}" Content="Delete" />

最新更新