简体   繁体   English

C# 将绑定替换为 x:Bind(将 UWP 迁移到 WinUI 3)

[英]C# replace Binding with x:Bind (Migrate UWP to WinUI 3)

I have a Page.Resources Datatemplate, there I specify an x:DataType我有一个 Page.Resources 数据模板,我在那里指定了一个 x:DataType

     <Page.Resources>
         <DataTemplate x:Key="DataPanelTemplate" x:DataType="dataModel:Activity">
    

the problem is in this DataTemplate I have this AppBarButton:问题出在这个 DataTemplate 我有这个 AppBarButton:

      <AppBarButton Icon="Stop" IsCompact="True" x:Name="cmdStopActivity" >
                    <Interactivity:Interaction.Behaviors>
                        <Core:EventTriggerBehavior EventName="Click">
                            <Core:InvokeCommandAction Command="{Binding ActivitiesViewModel.FinishActivityCommand, Source={StaticResource Locator}}" 
                                                      CommandParameter="{Binding}"/>
                        </Core:EventTriggerBehavior>
                    </Interactivity:Interaction.Behaviors>
                </AppBarButton>
  

How can I define the InvokeCommandAction Command as x:Bind?如何将 InvokeCommandAction 命令定义为 x:Bind? There I need access to the ViewModel and not to the specified x:DataType.我需要访问 ViewModel 而不是指定的 x:DataType。 I tryed this:我试过这个:

     xmlns:viewModel="using:PnxZeiterfassung.ViewModel"
     x:Bind viewModel:ActivitiesViewModel.FinishActivityCommand
      public RelayCommand<Activity> FinishActivityCommand { get; set; }
      FinishActivityCommand = new RelayCommand<Activity>(async (a) => await FinishActivity(a));

but here the FinishActivityCommand is not recognized.但这里无法识别 FinishActivityCommand。

In a DataTemplate , you can only use x:Bind to bind to a property of the the type specified by the x:DataType attribute.DataTemplate中,您只能使用x:Bind绑定到x:DataType属性指定类型的属性。

You cannot x:Bind to a view model property from inside a DataTemplate so, in this case, it's not possible or supported to use x:Bind to bind the CommandParameter property to a property of the view model. You should stick with the {Binding} markup.您不能从DataTemplate内部x:Bind到视图 model 属性,因此,在这种情况下,不可能或不支持使用x:BindCommandParameter属性绑定到视图 model 的属性。您应该坚持使用{Binding}标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM