【问题标题】:Setting WPF stackpanel background color with the same color as contained TextBlock background将 WPF 堆栈面板背景颜色设置为与包含的 TextBlock 背景相同的颜色
【发布时间】:2019-05-19 03:10:33
【问题描述】:

我有一个包含图像和文本块的堆栈面板。

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

我想将TextBlock背景色绑定到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>

【问题讨论】:

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


    【解决方案1】:

    我已经通过将 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>
    

    【讨论】:

    • 为什么不直接为 StackPanel 的背景设置动画,根本不设置 TextBlock 背景呢?这个解决方案看起来很糟糕。也没有理由为 StackPanel 的背景绑定使用元素语法。像往常一样写:Background="{Binding Background, ElementName=txtStoryboard}".
    • @Clemens 好的,我会尝试告诉你它是否有效。但我不确定您的解决方案是否可以通过仅将动画应用于堆栈面板来工作,因为除非文本块背景颜色设置为透明,否则堆栈面板的 texblock 背景颜色可能不会改变。
    • TextBlock 的背景当然必须是透明的或为空的。你当然不能要求人们“停止投票”。这不是“没有任何理由”。投反对票表明您没有写出好的问题或答案,并尝试改进它。人们不会为了好玩而投反对票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多