wpf - 控制模板可见性触发器

标签 wpf xaml data-binding custom-controls

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

它的编写方式甚至无法编译,找不到“Glyph”。我怎样才能清理这个按钮,使其在 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>

最佳答案

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

关于wpf - 控制模板可见性触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11000830/

相关文章:

c# - 在运行时将所有事件处理程序从一个控件复制到另一个控件

c# - WPF 窗口中的图像在通过反射启动时抛出异常

c# - WPF DataGrid 绑定(bind)困惑

WPF通过子容器滚动父容器

c# - 在 TreeView 中使用 HierarchicalDataTemplates

XAML,类似于 HTML 中的 float ?

c# - 使用 WPF 密码绑定(bind)在类型中找不到可附加属性

java - 在 null 上找不到值类型为 java.lang.String 的属性 'android:text' 的 getter

c# - Winrt,根据绑定(bind)值改变颜色

c# - 我的 UserControl 中的 DependencyProperty 无法更新 ViewModel 中的绑定(bind)属性