XAML 代码需要我在不同 VM 中的命令
The XAML code expect my Command in different VM
在主 window xaml 上,我有一个框架可以承载 2 个页面,就像这样
<Window x:Class="Monitor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" FontSize="14">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="48"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Frame Content="Frame" Grid.Row="1" Source="/Monitor;component/Views/Pages/GroupPage.xaml"/>
</Grid>
</Window>
首页(GroupPage.xaml)xaml代码为
<Page.DataContext>
<VM:GroupPageVM />
</Page.DataContext>
<Grid>
<ItemsControl x:Name="GroupsOfItemsControl" ItemsSource="{Binding GroupsOfItems}">
<ItemsControl.Template>
<ControlTemplate>
<WrapPanel Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
FlowDirection="LeftToRight" IsItemsHost="true">
</WrapPanel>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button HorizontalAlignment="Right" Margin="3"
Content="{Binding Name}"
Width="1.2in" Height=".75in" FontSize="14"
Command="{Binding Path=GroupSelectedCommand }"
CommandParameter= "{Binding}"
/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
GroupPageVM 是此页面的 VM。 GroupsOfItems 是组的 VM 集合。每个组 VM 都有一个名称,我将其绑定到代表该组的按钮的内容。所以在屏幕上,我将在 WrapPanel 中看到一组按钮。一切都正确显示。
现在问题出在处理按钮点击的命令上,代码期望我将我的命令 (GroupSelectedCommand) 及其执行函数放在组的 VM 中,而不是放在 MainWindow VM 中。
谁能解释一下为什么?
如果它应该如何工作,我该如何将命令放入 MainWindow VM?因为没有它,我无法通过单击其中一个按钮访问托管页面或任何 NavigationService 的框架以转到第 2 页。
(我的简单目标是点击代表一个组的按钮,它将导航到一个页面以显示该组中的项目)
您已将 GroupsOfItems
绑定为 ItemsControl
的 ItemsSource
。在 GroupsOfItems 中,您有 GroupVM
个对象。因此 ItemsControl
中的每个项目都将包含一个 GroupVM
对象。此外,定义的 DataTemplate
将代表 GroupVM
对象。因此,命令将在 GroupVM
对象内。
要访问父 DataContext,您可以尝试给出 ElementName
,如下所示,
Command="{Binding DataContext.GroupSelectedCommand, ElementName=GroupsOfItemsControl }"
在上面的代码中,按钮将尝试从 GroupPageVM
的 ItemsControl 的 DataContext 获取命令
在主 window xaml 上,我有一个框架可以承载 2 个页面,就像这样
<Window x:Class="Monitor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" FontSize="14">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="48"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Frame Content="Frame" Grid.Row="1" Source="/Monitor;component/Views/Pages/GroupPage.xaml"/>
</Grid>
</Window>
首页(GroupPage.xaml)xaml代码为
<Page.DataContext>
<VM:GroupPageVM />
</Page.DataContext>
<Grid>
<ItemsControl x:Name="GroupsOfItemsControl" ItemsSource="{Binding GroupsOfItems}">
<ItemsControl.Template>
<ControlTemplate>
<WrapPanel Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
FlowDirection="LeftToRight" IsItemsHost="true">
</WrapPanel>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button HorizontalAlignment="Right" Margin="3"
Content="{Binding Name}"
Width="1.2in" Height=".75in" FontSize="14"
Command="{Binding Path=GroupSelectedCommand }"
CommandParameter= "{Binding}"
/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
GroupPageVM 是此页面的 VM。 GroupsOfItems 是组的 VM 集合。每个组 VM 都有一个名称,我将其绑定到代表该组的按钮的内容。所以在屏幕上,我将在 WrapPanel 中看到一组按钮。一切都正确显示。
现在问题出在处理按钮点击的命令上,代码期望我将我的命令 (GroupSelectedCommand) 及其执行函数放在组的 VM 中,而不是放在 MainWindow VM 中。
谁能解释一下为什么? 如果它应该如何工作,我该如何将命令放入 MainWindow VM?因为没有它,我无法通过单击其中一个按钮访问托管页面或任何 NavigationService 的框架以转到第 2 页。
(我的简单目标是点击代表一个组的按钮,它将导航到一个页面以显示该组中的项目)
您已将 GroupsOfItems
绑定为 ItemsControl
的 ItemsSource
。在 GroupsOfItems 中,您有 GroupVM
个对象。因此 ItemsControl
中的每个项目都将包含一个 GroupVM
对象。此外,定义的 DataTemplate
将代表 GroupVM
对象。因此,命令将在 GroupVM
对象内。
要访问父 DataContext,您可以尝试给出 ElementName
,如下所示,
Command="{Binding DataContext.GroupSelectedCommand, ElementName=GroupsOfItemsControl }"
在上面的代码中,按钮将尝试从 GroupPageVM