使用与包含的TextBlock背景相同的颜色设置WPF stackpanel背景颜色

时间:2018-12-18 16:36:34

标签: c# wpf xaml binding .net-3.5

我有一个包含图像和TextBlock的堆栈面板。

TextBlock绑定到一种样式,该样式使其闪烁,其背景颜色从红色变为黑色,反之亦然。

我想将TextBlock backgorund颜色绑定到stackpanel背景颜色,也就是说,当TextBlock背景颜色为Red时,我需要stackpanel背景颜色为Red,而当TextBlock背景颜色为Black时,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 个答案:

答案 0 :(得分:-2)

我已经通过将TextBlock背景属性绑定到stackpanel背景属性来解决:

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