2014-02-26 99 views
0

我創建了一個用戶控件,在網格中有網格和很少的控件。我已經將Opacity設置爲我的父網格的.1,並且我想將後代控件的不透明度設置爲1.是否可以在XAML樹結構中執行? 這裏是我的XAML代碼:如何根據父控件的不透明度控制後代控件的不透明度?

<Grid Name="busyIndicatorGrid" Visibility="Visible" Opacity=".2" 
     Background="DarkBlue" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
    <StackPanel Orientation="Vertical" Opacity="1" 
       Background="Transparent" HorizontalAlignment="Center" VerticalAlignment="Center"> 
     <ProgressBar Name="progressBar" Foreground="White" 
        IsIndeterminate="True" MinWidth="100" Height="25" VerticalAlignment="Center"/> 
     <TextBlock Name="txtProgressText" 
        FontSize="40" 
        Margin="0,5,0,0" 
        Text="Please wait while application is being initialized." 
        TextAlignment="Left" TextWrapping="Wrap" /> 
    </StackPanel> 
</Grid> 

回答

0

感謝您的寶貴建議傢伙。但我已經找到了解決我的問題的方法,通過使用父母網格控件的畫筆。

<Grid Name="busyIndicatorGrid" Visibility="Visible" Opacity=".2" 
    HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
    <Grid.Background> 
     <SolidColorBrush Color="DarkBlue" Opacity="0.2"/> 
    </Grid.Background> 
<StackPanel Orientation="Vertical" 
      Background="Transparent" HorizontalAlignment="Center" VerticalAlignment="Center"> 
    <ProgressBar Name="progressBar" Foreground="White" 
       IsIndeterminate="True" MinWidth="100" Height="25" VerticalAlignment="Center"/> 
    <TextBlock Name="txtProgressText" 
       FontSize="40" 
       Margin="0,5,0,0" 
       Text="Please wait while application is being initialized." 
       TextAlignment="Left" TextWrapping="Wrap" /> 
</StackPanel> 

這樣它會繪製我已經在後臺屬性定義的不透明度父網格,它會對內部控制沒有影響。所有的控件將照常進行繪製,沒有在父柵格上設置的任何不透明度覆蓋效果。

0

不透明度爲將傳播下來的子樹的屬性。

如果父親的不透明度爲0.5,孩子的不透明度爲0.2,父母將被繪製爲50%,孩子將被繪製爲父母的50%的20%...有點像這樣的只是爲了更好的理解......我希望你讓我:)

嘗試了這一點,你會看到:

<Grid Name="busyIndicatorGrid" Visibility="Visible" Opacity=".2"> 
    <StackPanel Orientation="Vertical" Opacity="1" > 
    ... 

<Grid Name="busyIndicatorGrid" Visibility="Visible" Opacity=".2"> 
    <StackPanel Orientation="Vertical" Opacity="0.5"> 
    ... 

這兩個將受到不同的繪製這意味着孩子們沒有采取從其父的不透明度值,而是計算自己。

如果你想控制如何繪製內在的孩子,我會建議你創建一個附加的屬性,它沿着VisualTree繼承它的內容,當設置爲true時,它改變其「下面」關聯元素的不透明度到1或到他們以前的舊值。

下面是一個例子:

public static bool GetChangeOpacity(DependencyObject obj) 
    { 
     return (bool)obj.GetValue(ChangeOpacityProperty); 
    } 

    public static void SetChangeOpacity(DependencyObject obj, bool value) 
    { 
     obj.SetValue(ChangeOpacityProperty, value); 
    } 

    // Using a DependencyProperty as the backing store for ChangeOpacity. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ChangeOpacityProperty = 
     DependencyProperty.RegisterAttached("ChangeOpacity", typeof(bool), typeof(YourClass), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits, PropertyChangedCallback)); 

    private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) 
    { 
     FrameworkElement fe = dependencyObject as FrameworkElement; 
     if (fe != null && (bool)args.NewValue) 
     { 
      fe.Tag = fe.Opacity; 
      fe.Opacity = 1; 
     } 
     else if(fe != null && fe.Tag != null) 
     { 
      fe.Opacity = (double)fe.Tag; 
     } 
    } 
+0

感謝您的解釋:)你能否添加你解釋的代碼片段? –

+0

我剛剛做到了。看看它。 –

+0

我嘗試使用附加的行爲,因爲你說,但我沒有看到任何改變,就像我設置內部控件不透明度爲1時,我的行爲設置爲true,但問題仍然存在。父網格控件的不透明度設置爲.2,並且我希望看到內部控件完全可見而沒有應用任何不透明度。 –