如何禁用所有组件并更改背景颜色?

How can I disable all component and change background color?

我有一个 WPF 应用程序。当我按下退出按钮时,我希望看到这样的框:

盒子名称是Risultati。这有效,但我希望所有 WPF 应用程序都被禁用,我可以看到浅灰色的模态对话框。

这是我用来创建盒子的代码,Risutlati:

<UserControl x:Class="ContattoOculare.RiepilogoEsercizio"
             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="342" d:DesignWidth="520">
    <Grid Margin="0,0,0,0" Height="342" Width="520">
        <Grid.RowDefinitions>
            <RowDefinition Height="80" />
            <RowDefinition Height="35" />
            <RowDefinition Height="40" />
            <RowDefinition Height="100" />
            <RowDefinition Height="50" />
        </Grid.RowDefinitions>

        <Grid.Background>
            <ImageBrush x:Name="backgroudCarta" ImageSource="./Resources/Esci_dal_gioco_Maschera.png"/>
        </Grid.Background>

        <!--INTESTAZIONE-->
        <Grid Grid.Row="0" Margin="0,20,0,0" >
            <Grid.RowDefinitions>
                <RowDefinition Height="40" />
                <RowDefinition Height="32" />
            </Grid.RowDefinitions>
            <Grid Grid.Row="0">
                <Label Content="Risultati" VerticalAlignment="Bottom" HorizontalAlignment="Center" 
                   FontSize="30"  FontFamily="./Font/#Roboto-Bold" Foreground="White"/>
            </Grid>
            <Grid Grid.Row="1" Margin="0,0,0,14">
                <Label Content="Contatto Oculare" VerticalAlignment="Top" HorizontalAlignment="Center" 
               FontSize="18" FontFamily="./Font/#Roboto-Bold" Foreground="White" Margin="190,-12,184,0"/>
            </Grid>
        </Grid>
        <!--FINE INTESTAZIONE-->

        <!--PRIMA RIGA-->
        <Grid Grid.Row="1">
            <Label Content="Tempo"  FontSize="22"
               FontFamily="./Font/#Roboto-Bold" Foreground="Gray" HorizontalAlignment="Center"/>
        </Grid>
        <!--FINE PRIMA RIGA-->

        <!--SECONDA RIGA-->
        <Grid Grid.Row="2" Margin="0,-12,0,0">
            <Label x:Name="labelTempo" Content="0 sec"  FontSize="25" FontWeight="Bold"
               FontFamily="./Font/#Roboto-Bold" Foreground="Gray" HorizontalAlignment="Center"/>
        </Grid>
        <!--FINE SECONDA RIGA-->

        <!--TERZA RIGA-->
        <Grid Grid.Row="3" Margin="50,0,50,0">
            <TextBox x:Name="textDescrizione" VerticalAlignment="Top" FontSize="25"
               FontFamily="Calibri" Foreground="Gray"
               TextWrapping="Wrap" AcceptsReturn="True" Height="100" />
        </Grid>
        <!--FINE TERZA RIGA-->

        <!--QUARTA RIGA-->
        <Grid Grid.Row="4" HorizontalAlignment="Center" Margin="0,5,0,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Grid Height="Auto" Width="Auto" HorizontalAlignment="Center"
                     Grid.Column="0" >
                <Image Width="206" Height="46"
                               HorizontalAlignment="Center" VerticalAlignment="Bottom"
                            x:Name="save">
                <Image.Style>
                    <Style TargetType="{x:Type Image}">
                            <Setter Property="Source" Value="./Resources/Tasto_Uscita gioco_Salva_Roll_ON.png"/>
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Source" Value="./Resources/Tasto_Uscita_gioco_Roll_OFF.png"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Image.Style>
                </Image>
                <Label HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Roboto-Bold"
                           FontSize="20" Foreground="White">Salva</Label>
            </Grid>

            <Grid Height="Auto" Width="Auto" HorizontalAlignment="Center"
                     Grid.Column="1" >
                <Image Width="206" Height="46"
                               HorizontalAlignment="Center" VerticalAlignment="Bottom"
                            x:Name="stop">
                    <Image.Style>
                        <Style TargetType="{x:Type Image}">
                            <Setter Property="Source" Value="./Resources/Tasto_Uscita_gioco_Salva_Roll_OFF.png"/>
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Source" Value="./Resources/Tasto_Uscita_gioco_Roll_OFF.png"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Image.Style>
                </Image>
                <Label HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Roboto-Bold"
                           FontSize="20" Foreground="White">Annulla</Label>
            </Grid>
        </Grid>
        <!--FINE QUARTA RIGA-->
    </Grid>
</UserControl>

我该怎么做?

对于任何 WPF window,我通常会对其内容实施遮罩,例如:

<Window>
    <Window.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibility"/>
    </Window.Resources>
    <Grid>
        <Grid>
            <!--your window content-->
        </Grid> 

        <!--this is your mask, give it a semi-transparent color-->
        <Grid Background="#65000000" Visibility="{Binding IsModalDialogActived, Converter={StaticResource BooleanToVisibility}}"/>

         <Grid>
            <!--your dialog-->
        </Grid> 
    </Grid>
</Window>

这样您就可以在显示模态对话框时将遮罩设置为可见,并且遮罩会在界面上投下阴影并阻止任何用户与您的应用程序交互。

我会扩展 Window class 而不是 UserControl class。然后,调用 ShowDialog 来显示您的框。应用程序中的所有其他 windows 将被禁用。这是一个真正的 (WPF) 模式对话框,松散耦合并可在您的应用程序中重复使用。

牢记:

  • 使用 AllowsTransparency="True" 允许背景使用半透明颜色。
  • 使用 WindowStyle="None" 删除边框。
  • 使用WindowState="Maximized"占用所有可用的space.
  • 在主网格中设置 Horizo​​ntalAlignment、VerticalAlignment、Width、Height 和 Margin 属性,以将框放置在需要的位置。