代码之家  ›  专栏  ›  技术社区  ›  Willy

将WPF stackpanel背景色设置为与包含的TextBlock背景色相同的颜色

  •  0
  • Willy  · 技术社区  · 6 年前

    我有一个包含图像和文本块的stackpanel。

    TextBlock被绑定到一个样式,该样式使其闪烁,背景颜色从红色变为黑色,并交替显示为viceversa。

    我想将TextBlock backgorund color绑定到stackpanel背景色,即当TextBlock背景色为红色时,需要stackpanel背景色为红色,当TextBlock背景色为黑色时,则stackpanel背景色必须更改为黑色,依此类推。。。

    以下是我的代码:

    <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>
    
    1 回复  |  直到 6 年前
        1
  •  -2
  •   Willy    6 年前

    我已通过将TextBlock background属性绑定到stackpanel background属性来解决此问题:

    <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>
    
    推荐文章