C#用X替换绑定:bind(将UWP迁移到Winui 3)
我有一个page.resources dataTemplate,在那里指定了一个x:datatype。
<Page.Resources>
<DataTemplate x:Key="DataPanelTemplate" x:DataType="dataModel:Activity">
问题是在此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>
如何将InvokeCommandAction命令定义为x:bind?在那里,我需要访问ViewModel,而不是指定的X:DataType。我尝试了一下:
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));
但是在这里尚未认识到FinishActivityCommand。
I have a Page.Resources Datatemplate, there I specify an x:DataType
<Page.Resources>
<DataTemplate x:Key="DataPanelTemplate" x:DataType="dataModel:Activity">
the problem is in this DataTemplate I have this 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? There I need access to the ViewModel and not to the specified 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
datatemplate
中,您只能使用x:bind
绑定到x:datatype
属性的类型的属性。您不能
x:bind
从datatemplate
内部绑定到视图模型属性,因此,在这种情况下,使用x:bind 将
命令参数
属性绑定到视图模型的属性。您应该坚持使用{binding}
标记。In a
DataTemplate
, you can only usex:Bind
to bind to a property of the the type specified by thex:DataType
attribute.You cannot
x:Bind
to a view model property from inside aDataTemplate
so, in this case, it's not possible or supported to usex:Bind
to bind theCommandParameter
property to a property of the view model. You should stick with the{Binding}
markup.