首页 文章

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

提问于
浏览
-1

我的主窗口 view

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

View model

private bool isContentBlocked = false;        

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

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

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

最初,当我启动WPF应用程序时,应该隐藏顶部面板,以便启用所有内容,用户应该可以使用它们但是由于某种原因,顶部面板没有被隐藏,显示,所以所有内容都无法访问 . 我使用一个矩形使内容成为阻塞 .

我究竟做错了什么?

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

1 回答

  • -1

    如果您的 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>
    

相关问题