StackPanel可见性VS网格可见性

时间:2013-06-18 07:42:10

标签: wpf mvvm visibility

我正在使用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>

能见度很好。

为什么?

1 个答案:

答案 0 :(得分:2)

DataContext的{​​{1}}为GridCurrentMonitorAlert的{​​{1}}为DataContext。因此,绑定到StackPanel正在解决MonitorAlertViewModel案例中的错误内容。

在整个视图中设置NoMonitorsMessageVisibility就像是非正统的。通常在执行MVVM时,让WPF处理设置Grid(可能在根级别除外)并在必要时在绑定中使用更深的路径。您可能想考虑采用这种方法。