MVVM 多重绑定问题
我有以下 XAML 布局,
<DataTemplate x:Key="Reports">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title}" Grid.Column="1"/>
<telerik:RadButton x:Name="Edit"
Command={Binding MyCommand} <!-- From View Model -->
CommandParameter={Binding Id}/> <!-- From DataTemplate -->
</Grid>
</DataTemplate>
我想将命令绑定到按钮,这需要我将按钮的数据上下文设置为 ViewModel。
但我想将 DataTemplate 数据上下文中的数据绑定到命令参数。
同一控件内是否可以有两个数据上下文?
I have the following XAML layout
<DataTemplate x:Key="Reports">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title}" Grid.Column="1"/>
<telerik:RadButton x:Name="Edit"
Command={Binding MyCommand} <!-- From View Model -->
CommandParameter={Binding Id}/> <!-- From DataTemplate -->
</Grid>
</DataTemplate>
I would like to bind a command to the button which would require me to set the data context of the button to the ViewModel.
But I would like to bind data from the DataTemplate data context to the command parameter.
Is is possible to have two data contexts within the same control?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,不是。但是您可以将绑定关联到视图模型中的命令:
这里“Root”是您使用此代码的用户控件或页面的名称:
此页面将绑定到您的视图模型,因此您可以使用 DataContext访问它。这就是您在绑定中使用路径
DataContext.MyCommand
的原因。最后,在您的视图模型中,您应该有以下命令:
No, it's not. But you can associate the binding to a command in your viewmodel:
Here "Root" is the name of the user control or page where you are using this code:
This page would be bound to your view model, so you can use the DataContext to access it. That's why you use the path
DataContext.MyCommand
in the binding.Finally, in your viewmodel, you should have the command: