WPF MVVM:阻止所有控件的顶部面板

WPF MVVM: Top panel to block all controls

我的主要window查看:

<Window.Resources>
       <BooleanToVisibilityConverter x:Key="BoolToVisibility"/>
</Window.Resources>

<Grid x:Name="TopGrid">
    <Rectangle x:Name="TopPanel" Grid.ZIndex="3" 
               Fill="LightBlue"  Opacity="0.3" 
               Visibility="{Binding IsContentBlocked, Mode=TwoWay, Converter={StaticResource BoolToVisibility}}" />
    <Grid Name="main"  DataContext="{StaticResource mainViewModel}">

        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1.2*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <!-- Here my controls: stackpanel, buttons, groupboxes, etc. -->
   </Grid>
</Grid>

查看模型:

    private bool isContentBlocked = false;        

    public bool IsContentBlocked
    {
        get
        {
            return this.isContentBlocked;
        }

        set
        {
            if (this.isContentBlocked == value)
            {
                return;
            }

            this.isContentBlocked = value;
            OnPropertyChanged("IsContentBlocked");
        }
    }

最初当我启动 WPF 应用程序时,顶部面板应该被隐藏,所以所有内容都应该被启用,用户应该能够使用它们,但由于某种原因,顶部面板没有被隐藏,它被显示了,所以无法访问所有内容。我用一个矩形来让内容被遮挡起来。

我做错了什么?

我的视图模型正确地实现了 INotifyPropertyChanged。

假设你的IsContentBlocked属性是在mainViewModel中定义的,你应该把TopGridDataContext设置成这个

<Grid x:Name="TopGrid" DataContext="{StaticResource mainViewModel}">
    <Rectangle x:Name="TopPanel" Grid.ZIndex="3" 
               Fill="LightBlue"  Opacity="0.3" 
               Visibility="{Binding IsContentBlocked, Converter={StaticResource BoolToVisibility}}" />
    <Grid Name="main">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1.2*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <!-- Here my controls: stackpanel, buttons, groupboxes, etc. -->
    </Grid>
</Grid>