来自同一视图 WPF MVVM 中多个 ViewModel 的绑定

Bindings from Multiple ViewModels in same View WPF MVVM

我有一个 WPF MVVM 项目,其中包含一个包含多个控件的 View

有一个 DockPanel,其中包含 View 中的所有控件,DataContext 到 MainViewModel Class,在它里面有一个 Grid 和 DataContext 到另一个对象 ViewModel 它包含在 属性在 MainViewModel Class.

现在,在 Grid 我有一个 2 个按钮 环绕 WrapPanel,按钮有一个 属性 的命令,它写在 MainViewModel Class 中,当我按下它时什么也没有发生(因为 Grid 的 DataContext 是针对其他对象的)。

我需要将命令保留在 MainViewModel Class 中,我该怎么做?

XAML:(仅相关部分)

<DockPanel x:Name="MonitorParent" DataContext="{Binding MonitorMainViewModel}" LastChildFill="False" Width="1144" HorizontalAlignment="Left">
      .......     
    <StackPanel>
        <ContentControl Visibility="{Binding Path=NoMonitorsMessageVisibility, Converter={StaticResource visibilityConverter}}">
            <StackPanel Width="804" Height="574">
                <Grid DataContext="{Binding CurrentMonitor}" Margin="10 0" Height="auto">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" MinHeight="23" />
                        <RowDefinition Height="Auto" MinHeight="23" />
                        <RowDefinition Height="Auto" MinHeight="23" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="140" />
                    </Grid.ColumnDefinitions>
                    <Label Grid.Column="0" Grid.Row="15"  Content="Serial Number" x:Name="SerialNumber"   />
                    <WrapPanel  DataContext="{Binding MonitorMainViewModel}" HorizontalAlignment="Center" Margin="0,10,0,10" Grid.ColumnSpan="2" Grid.Row="6" >
                        <Button Content="Add" Width="60" Margin="0,0,40,0" Command="{Binding AddConfidenceCommand}" />
                        <Button Content="Delete" Width="60" Margin="30,0,0,0" Command="{Binding DeleteConfidenceCommand}"/>
                    </WrapPanel>

                     ........
  </DockPanel

试试这个:

<Button Content="Add" Width="60" Margin="0,0,40,0" 
        Command="{Binding DataContext.AddConfidenceCommand, ElementName=MonitorParent}" />

或者:

<Button Content="Add" Width="60" Margin="0,0,40,0" 
        Command="{Binding DataContext.AddConfidenceCommand, RelativeSource={RelativeSource AncestorType=DockPanel}}" />

您可以像这样使用 DockPanel 的名称进行绑定:

<DockPanel x:Name="MonitorParent" DataContext="{Binding MonitorMainViewModel}" LastChildFill="False" Width="1144" HorizontalAlignment="Left">
  .......     
<StackPanel>
    <ContentControl Visibility="{Binding Path=NoMonitorsMessageVisibility, Converter={StaticResource visibilityConverter}}">
        <StackPanel Width="804" Height="574">
            <Grid DataContext="{Binding CurrentMonitor}" Margin="10 0" Height="auto">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" MinHeight="23" />
                    <RowDefinition Height="Auto" MinHeight="23" />
                    <RowDefinition Height="Auto" MinHeight="23" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="140" />
                </Grid.ColumnDefinitions>
                <Label Grid.Column="0" Grid.Row="15"  Content="Serial Number" x:Name="SerialNumber"   />
                <WrapPanel  DataContext="{Binding MonitorMainViewModel}" HorizontalAlignment="Center" Margin="0,10,0,10" Grid.ColumnSpan="2" Grid.Row="6" >
                    <Button Content="Add" Width="60" Margin="0,0,40,0" Command="{Binding ElementName=MonitorParent, Path=DataContext.AddConfidenceCommand}" />
                    <Button Content="Delete" Width="60" Margin="30,0,0,0" Command="{Binding ElementName=MonitorParent, Path=DataContext.DeleteConfidenceCommand}"/>
                </WrapPanel>

                 ........