我有一个WPF MVVM项目,该项目包括一个包含多个控件的View。

有一个 DockPanel ,它包含具有MainContextModel类的带DataContext的 View 中的所有控件,并且在其内部有一个具有DataContext的网格,该网格具有MainViewModel类中包含的其他对象ViewModel的属性。

现在,在该网格中,我有一个 2个按钮周围有 WrapPanel ,并且这些按钮都有一个Commands到在MainViewModel类中编写的属性,当我按下它时,不会发生任何事情(因为Grid的DataContext到其他对象)。

我需要将Commands保留在MainViewModel类中,该怎么做?

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

最佳答案

您可以使用DockPanel的名称进行绑定(bind),如下所示:

<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>

                 ........

关于c# - 来自同一View WPF MVVM中多个ViewModel的绑定(bind),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44408487/

10-13 01:47