Wpf 模式窗口透明度

Wpf 模式窗口透明度,wpf,xaml,styling,Wpf,Xaml,Styling,我创建了一个模态WPF窗口,如下所示: MainGrid.Children.Add(uc); <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="YourDesiredSize"/> <ColumnDefinition/> </Grid.ColumnDefiniti

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

MainGrid.Children.Add(uc);
<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>

以下是该窗口的代码:

<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>
问题是,一旦展开堆栈跟踪,控件透明度也会发生变化:

我假设这与使用不正确透明度的
ScrollViewer
有关,即使用
矩形而不是包含
窗口的

我还将拥有
ScrollViewer
UserControl的
Opacity
设置为1,然后绑定
Opacity

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

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

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

好吧,我找到了一个适合我的解决方案,我相信这不是最好的,但它可能会帮助那些和我有同样问题的人

问题是,我添加到
窗口的
UserControl
中的控件是透明的,尽管我无法找出原因,但我找到了一个简单的解决方法

通过将
UserControl
OpacityMask
属性更改为所需的
Background
颜色,即使控件的不透明度发生变化,也会使用您提供的
笔刷将其遮罩

uc.OpacityMask = Brushes.WhiteSmoke;

希望它能帮助别人

您好,我相应地更改了代码,问题仍然是我的实际
UserControl
仍然是透明的!尝试将一个
ScrollViewer
插入您的
contentPlaceHolder
,如果我没有弄错的话,它也应该这样做!我试过了,但仍然不透明。我已经更新了截图。您的代码(我想是
ElementBinding
DataTemplates
)中还有什么其他内容可能会在
Expander
打开时导致背景更改?我已经用我的用户控件的代码更新了我的问题,您可以在那里查看!就我所见,那里没有什么不寻常的事。什么是
odc:OdcExpander
?就像我说的,我感觉它和那个元素有关(当扩展器打开时,透明度出现)
contentPlaceHolder.Children.Add(uc);
uc.OpacityMask = Brushes.WhiteSmoke;