【问题标题】:WPF TextBlock BackGround not being set using style未使用样式设置 WPF TextBlock 背景
【发布时间】:2013-02-27 02:27:05
【问题描述】:

我正在学习 WPF。我试图使用Style.TriggerTextBlock 申请BackgroundForeground。从我定义的Trigger 中,我可以注意到MouseOver 上的前景正在更改,但Background 没有。你能帮忙吗?下面是我的 XAML:

<Window x:Class="WpfApplication1.ApplyingStyle"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ApplyingStyle"
        Height="300"
        Width="300">
  <Window.Resources>
    <Style x:Key="myStyle">
      <Setter Property="Control.Background"
              Value="Red" />
      <Setter Property="Control.FontFamily"
              Value="Times New Roman" />
      <Setter Property="Control.FontSize"
              Value="25" />
      <Style.Triggers>
        <Trigger Property="Control.IsMouseOver"
                 Value="True">
          <Setter Property="Control.Foreground"
                  Value="HotPink" />
          <Setter Property="Control.Background"
                  Value="Blue" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </Window.Resources>
  <Grid ShowGridLines="True">
    <Grid.RowDefinitions>
      <RowDefinition   Height="*" />
      <RowDefinition />
      <RowDefinition />
    </Grid.RowDefinitions>
    <TextBlock Name="myTextBlock"
               Grid.Row="1"
               Grid.Column="0"
               Style="{StaticResource myStyle}">Hello</TextBlock>
  </Grid>
</Window>

【问题讨论】:

    标签: wpf textblock


    【解决方案1】:

    我建议使用Style.TargetType,这样可以

    1. 缩短Setter.Property
    2. 防止普通属性和附加属性之间出现歧义
    3. 如果目标类型实际上具有该属性,请进行一些 IDE 验证

    TextBlock 不是 Control 这就是为什么这不起作用。使用 TargetType="TextBlock" 并将 Control.Background 更改为 Background 或使用 TextBlock.Background

    (使用样式时还要注意value precedence,如果您在TextBlock 本身上设置Background,则您有一个完全覆盖样式的本地值

    【讨论】:

      【解决方案2】:

      尝试使用您尝试在样式声明中应用样式的控件类型,而不是使用Control.Background 使用TextBlock.Background 或将TargetType 设置为TextBlock。当我将您的样式设置为这样时,它似乎可以工作。

      <Window x:Class="WpfApplication1.ApplyingStyle"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              Title="ApplyingStyle" Height="300" Width="300">
          <Window.Resources>
              <Style x:Key="myStyle" TargetType="TextBlock">
                  <Setter Property="Background" Value="Red" />
                  <Setter Property="FontFamily" Value="Times New Roman" />
                  <Setter Property="FontSize" Value="25" />
                  <Style.Triggers>
                      <Trigger Property="IsMouseOver" Value="True"  >
                          <Setter Property="Background" Value="Blue" />
                          <Setter Property="Foreground" Value="HotPink" />
                      </Trigger>
                  </Style.Triggers>
              </Style>
          </Window.Resources>
      
          <Grid ShowGridLines="True" >
              <Grid.RowDefinitions>
                  <RowDefinition   Height="*" />
                  <RowDefinition/>
                  <RowDefinition />
              </Grid.RowDefinitions>
      
              <TextBlock Name="myTextBlock"  
                         Grid.Row="1" 
                         Grid.Column="0" 
                         Style="{StaticResource myStyle}">Hello</TextBlock>
          </Grid>
      </Window>
      

      【讨论】:

      • 谢谢马克。我试过这样做,但没有效果。我有什么明显的遗漏吗?
      • @NainilPatel 它正在为我使用您的示例代码。我还进行了编辑,看看它现在是否有效。我刚刚发布了您的示例,我通过更改进行了修改。这确实有效。
      猜你喜欢
      • 2019-04-07
      • 2012-09-05
      • 2018-11-18
      • 2016-07-24
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      相关资源
      最近更新 更多