控制模板可见性触发器

时间:2012-06-12 16:29:20

标签: wpf xaml data-binding custom-controls

我尝试使用下面的按钮样式只是在IsMouseOver或IsPressed时只显示按钮。

它的编写方式甚至不会编译,也不会找到“雕文”。当IsMoueOver?

时,如何清除按钮是否可见

干杯,
Berryl

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

1 个答案:

答案 0 :(得分: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>
相关问题