简体   繁体   中英

Setting WPF stackpanel background color with the same color as contained TextBlock background

I have a stackpanel that contains an image and a TextBlock.

TextBlock is bound to a style which makes it blink alternating its background color from Red to Black and viceversa.

I would like to bind the TextBlock backgorund color to stackpanel background color, that is, when TextBlock background color is Red I need stackpanel background color to be Red, and when TextBlock background color is Black, then stackpanel background color must change to Black and so on...

Below my code:

<Border Visibility="{Binding Path=BlinkOn, Converter={StaticResource BoolToVis}}" BorderThickness="1" BorderBrush="Red" CornerRadius="5" Margin="5">
  <StackPanel Orientation="Horizontal" Width="auto" Background="Red">
    <Image Width="24" Height="24" Source="/My.Images;component/Warning.png" />                    
    <TextBlock x:Name="lblStoryboard"
               TextAlignment="Center"
               Padding="5"                                                         
               Width="Auto"    
               Background="Red"
               Foreground="Black"
               FontSize="12.5"
               FontWeight="Bold"
               Style="{StaticResource BlinkingTextBlock}"
               Text="Hi there!" 
               TextWrapping="WrapWithOverflow"
               Visibility="{Binding Path=BlinkOn, Converter={StaticResource BoolToVis}}">
    </TextBlock>
 </StackPanel>
</Border>

I have solved by binding TextBlock background property to stackpanel background property:

<Border Visibility="{Binding Path=BlinkOn, Converter={StaticResource BoolToVis}}" BorderThickness="1" BorderBrush="Red" CornerRadius="5" Margin="5">
  <StackPanel Orientation="Horizontal" Width="auto">
    <StackPanel.Background>
        <Binding ElementName="txtStoryboard" Path="Background"/>
    </StackPanel.Background>
    <Image Width="24" Height="24" Source="/My.Images;component/Warning.png" />                    
    <TextBlock x:Name="txtStoryboard"
               TextAlignment="Center"
               Padding="5"                                                         
               Width="Auto"    
               Background="Red"
               Foreground="Black"
               FontSize="12.5"
               FontWeight="Bold"
               Style="{StaticResource BlinkingTextBlock}"
               Text="Hi there!" 
               TextWrapping="WrapWithOverflow"
               Visibility="{Binding Path=BlinkOn, Converter={StaticResource BoolToVis}}">
    </TextBlock>
 </StackPanel>
</Border>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM