2

我想要对下面的按钮样式做的只是让按钮仅在 IsMouseOver 或 IsPressed 时可见。

它的编写方式甚至无法编译,找不到“字形”。当 IsMoueOver 时,我怎样才能清理这个按钮是可见的?

干杯,
贝里尔

<Style x:Key="EditCommandButtonStyle" TargetType="{x:Type Button}">
   <Setter Property="Content">
      <Setter.Value>
         <TextBlock x:Name="Glyph" Width="30" 
            FontFamily="Wingdings 3" FontSize="24" Text="a" Visibility="Hidden"/>
      </Setter.Value>
   </Setter>
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type Button}">
            <Border x:Name="Border" Background="Transparent" CornerRadius="4">
               <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
            <ControlTemplate.Triggers>
               <Trigger Property="IsMouseOver" Value="True">
                  <Setter TargetName="Border" Property="Background" Value="LightBlue"/>
                  <Setter TargetName="Glyph" Property="Visibility" Value="Visible"/>
               </Trigger>
               <Trigger Property="IsPressed" Value="True">
                  <Setter TargetName="Border" Property="Background" Value="Orange"/>
               </Trigger>
            </ControlTemplate.Triggers>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>
4

1 回答 1

5

使 ContentPresenter 成为命名目标,而不是 TextBlock。

<Style x:Key="EditCommandButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Content">
        <Setter.Value>
            <TextBlock FontFamily="Wingdings 3" FontSize="24" Text="a" />
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="Border" Background="Transparent" CornerRadius="4">
                    <ContentPresenter x:Name="theContent" Visibility="Hidden"
                           HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="Border" Property="Background" 
                                Value="LightBlue"/>
                        <Setter TargetName="theContent" Property="Visibility" 
                                Value="Visible"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="Border" Property="Background" 
                                Value="Orange"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
于 2012-06-12T20:36:31.750 回答