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

时间:2015-04-08 14:07:07

标签: wpf c#-4.0

我有一个WPF应用程序。当我按下退出按钮时,我想看到如下框:

enter image description here

框名是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>

我该怎么做?

2 个答案:

答案 0 :(得分:1)

对于任何WPF窗口,我通常会在其内容上实现一个掩码,例如:

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

因此,当您显示模态对话框时,您可以将蒙版设置为Visible,并且蒙版将在界面上投下阴影并阻止任何用户与您的应用程序进行交互。

答案 1 :(得分:0)

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

请记住:

  • 使用allowsTransparency =“True”允许背景的半透明颜色。
  • 使用WindowStyle =“无”删除边框。
  • 使用WindowState =“Maximized”占用所有可用空间。
  • 在主网格中设置Horizo​​ntalAlignment,VerticalAlignment,Width,Height和Margin属性,将您的框放置在所需的位置。