0

我正在使用 MVVM 应用程序,有些东西不明白。 StackPanel 可见性和网格可见性有什么区别。如果我有这个网格......

 <UserControl x:Class="Envitech.Setup.Presentation.Views.MonitorScreenViews.MonitorAlertViews.MonitorAlertView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="438" d:DesignWidth="842" xmlns:popup="clr-namespace:Envitech.Setup.Presentation.Views.GlobalViews">
    <DockPanel DataContext="{Binding MonitorAlertViewModel}" Width="824" HorizontalAlignment="Left" VerticalAlignment="Top" Height="435">                        
        <Grid DataContext="{Binding CurrentMonitorAlert}" Height="422" Visibility="{Binding Path=NoMonitorsMessageVisibility, Converter={StaticResource visibilityConverter}}">
            <Label Content="Value" Height="28" HorizontalAlignment="Left" Margin="10,103,0,0"  VerticalAlignment="Top" />            
        </Grid>
    </DockPanel>
</UserControl>

能见度不起作用,但如果我这样做......

 <UserControl x:Class="Envitech.Setup.Presentation.Views.MonitorScreenViews.MonitorAlertViews.MonitorAlertView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="438" d:DesignWidth="842" xmlns:popup="clr-namespace:Envitech.Setup.Presentation.Views.GlobalViews">
    <DockPanel DataContext="{Binding MonitorAlertViewModel}" Width="824" HorizontalAlignment="Left" VerticalAlignment="Top" Height="435">                
        <StackPanel Visibility="{Binding Path=NoMonitorsMessageVisibility, Converter={StaticResource visibilityConverter}}">
        <Grid DataContext="{Binding CurrentMonitorAlert}" Height="422">
            <Label Content="Value" Height="28" HorizontalAlignment="Left" Margin="10,103,0,0"  VerticalAlignment="Top" />            
        </Grid>
        </StackPanel>       
    </DockPanel>
</UserControl>

能见度工作得很好。

为什么?

4

1 回答 1

2

的是。DataContext_ 的是。_ 因此,绑定到解决您的案例中的错误问题。GridCurrentMonitorAlertDataContextStackPanelMonitorAlertViewModelNoMonitorsMessageVisibilityGrid

在你的视野中设置DataContext类似的东西有点不正统。通常在执行 MVVM 时,您让 WPF 处理设置DataContext(可能在根级别除外)并在必要时在绑定中使用更深的路径。您可能要考虑采用这种方法。

于 2013-06-19T07:16:15.597 回答