2013-02-20 97 views
1

我創建瞭如下所示模態WPF窗口:WPF模態窗口透明度

enter image description here

下面是窗口中的代碼:

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

的問題,只要我展開堆棧跟蹤,控件透明度也隨之變化:

enter image description here

我假定這事做與使用不正確的透明度ScrollViewer,即在Rectangle而不是包含Window

我還設置其擁有ScrollViewer爲1 UserControlOpacity,然後綁定的Opacity

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

誰能幫助我?

-

UPDATE

這裏是插入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> 

回答

0

UserControl代碼如果你的窗口有一個固定的大小,不能調整大小,您可以使用以下伎倆:

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

^h H但是,這是不可能的,你的窗口將始終具有相同的大小,因此使其更有活力,你可以改變窗口的佈局如下:

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

放置在另一個上面的這個窗口的結果看起來或多或少是這樣的:

transparent scrollviewer

,然後,而不是增加了MainGrid的用戶控件添加到contentPlaceHolder或無論你怎麼稱呼它:

contentPlaceHolder.Children.Add(uc); 
+0

嗨,我相應地改變了我的代碼,問題仍然存在,我的實際'UserControl'仍然是透明的! – 2013-02-20 12:39:40

+0

嘗試在你的'contentPlaceHolder'中插入'ScrollViewer',如果我沒有弄錯它應該這樣做! – 2013-02-20 12:43:40

+0

我試過了,它仍然不透明。我更新了截圖。你的代碼還有其他什麼東西(我在考慮'ElementBinding'或'DataTemplates'),這可能會導致後臺在Expander打開後立即改變? – RoelF 2013-02-20 12:49:42

0

好的,所以我找到了一個適合我的解決方案,我相信這不是最好的解決方案,但它可能會幫助像我一樣遇到同樣問題的人。

問題在於我添加到我的WindowUserControl內的控件是透明的,但我找不到原因,我找到了一個簡單的解決方法。

通過改變UserControl到任何所需的Background顏色的OpacityMask財產,即使控制不透明度的變化,將與您提供的Brush掩蓋。

uc.OpacityMask = Brushes.WhiteSmoke; 

希望它可以幫助別人!