1

我创建了一个模态 WPF 窗口,如下所示:

在此处输入图像描述

这是窗口的代码:

<Window x:Class="Dionysus.Core.Controls.ModalWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ModalWindow" AllowsTransparency="True" Background="Transparent" WindowStyle="None">

<Grid Name="MainGrid">
    <Rectangle Fill="Gray" Opacity="0.7" />
</Grid>

然后添加“ErrorControl”,如下所示:

MainGrid.Children.Add(uc);

问题是,只要我展开堆栈跟踪,控件的透明度也会发生变化:

在此处输入图像描述

我假设这与ScrollViewer使用不正确的透明度有关,即,Rectangle而不是包含Window

我还将拥有1 的Opacityof设置为 1,然后绑定:UserControlScrollViewerOpacity

<ScrollViewer Background="WhiteSmoke" Opacity="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=Opacity}">

谁能帮我?

--

更新

UserControl是插入到Window

<Grid x:Name="LayoutRootx" Background="WhiteSmoke">
        <StackPanel VerticalAlignment="Stretch">
            <TextBlock TextWrapping="Wrap" Margin="5" Text="An error has occured:" Foreground="Black" FontSize="15" FontWeight="Medium"/>
            <TextBlock TextWrapping="Wrap" Margin="5,10,5,5" Text="{Binding Error}"/>
            <odc:OdcExpander Header="Stack Trace" Margin="5" IsExpanded="False" Background="WhiteSmoke">
                <TextBox Text="{Binding StackTrace}" TextWrapping="Wrap" Margin="5,10,5,5" IsReadOnly="True" MaxHeight="370"/>
            </odc:OdcExpander>
            <odc:OdcExpander Header="Comment" Margin="5" IsExpanded="False">
                <TextBox Text="{Binding Comment}" TextWrapping="Wrap" Margin="5,10,5,5" MaxHeight="370" Name="txtComment"/>
            </odc:OdcExpander>
            <StackPanel Margin="5,10,5,5" Orientation="Horizontal" HorizontalAlignment="Left">
                <Button Style="{StaticResource DionysusButton}"  Width="100" Height="23" IsDefault="True" Name="btnSendError">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="/Dionysus.Shell;component/Images/camera-icon.png" Margin="0,0,5,0">

                        </Image>
                        <TextBlock Text="Send to IT" VerticalAlignment="Center"/>
                        <core:DionysusTriggerAction Height="0" Width="0" TargetControl="{Binding ElementName=btnSendError}" MethodName="SendError"></core:DionysusTriggerAction>
                    </StackPanel>
                </Button>
                <Button Style="{StaticResource DionysusButton}"  Width="100" Height="23" Name="btnExit" Margin="10,0,0,0" IsCancel="True">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="/Dionysus.Shell;component/Images/DeleteRed.png" Margin="0,0,5,0">

                        </Image>
                        <TextBlock Text="Close" VerticalAlignment="Center"/>
                    </StackPanel>
                </Button>
                <core:DionysusTriggerAction Height="0" Name="triggerAction2" Width="0" TargetControl="{Binding ElementName=btnExit}" MethodName="Exit"></core:DionysusTriggerAction>
            </StackPanel>
        </StackPanel>
    </Grid>
4

2 回答 2

0

如果您的窗口具有固定大小且无法调整大小,则可以使用以下技巧:

<Grid>
    <Border BorderThickness="100" BorderBrush="Gray" Opacity="0.7">
        <Grid Background="White" Grid.Column="1" Grid.Row="1" x:Name="contentPlaceHolder">
            <TextBlock  Text="HELLO WORLD" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Grid>
    </Border>
</Grid>

但是,您的 Window 不太可能始终具有相同的大小,因此为了使其更具动态性,您可以按如下方式更改 Window 的布局:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="YourDesiredSize"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="YourDesiredSize"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Rectangle Fill="Gray" Opacity="0.7" Grid.Row="0" Grid.ColumnSpan="3"/>
    <Rectangle Fill="Gray" Opacity="0.7" Grid.Row="2" Grid.ColumnSpan="3"/>
    <Rectangle Fill="Gray" Opacity="0.7" Grid.Row="1" Grid.Column="0"/>
    <Rectangle Fill="Gray" Opacity="0.7" Grid.Row="1" Grid.Column="2"/>

    <Grid Grid.Column="1" Grid.Row="1" Background="White" x:Name="contentPlaceHolder">
        <TextBlock  Text="HELLO WORLD" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</Grid>

这个窗口放在另一个窗口上的结果或多或少是这样的:

透明滚动查看器

然后不是添加到MainGrid,而是将 UserControl 添加到contentPlaceHolder或者您想要调用它:

contentPlaceHolder.Children.Add(uc);
于 2013-02-20T12:16:08.697 回答
0

好的,所以我找到了一个适合我的解决方案,我确信它不是最好的,但它可能会帮助遇到与我相同问题的人。

问题是UserControl我添加到我的控件Window是透明的,虽然我无法弄清楚原因,但我找到了一个简单的解决方法。

通过将 的OpacityMask属性更改UserControl为所需的Background颜色,即使控件的不透明度发生变化,它也会被Brush您提供的 所掩盖。

uc.OpacityMask = Brushes.WhiteSmoke;

希望它可以帮助某人!

于 2013-02-22T09:35:06.910 回答