XAML 在MaterialDesign中使用触发器:PackIcon Kind=“Outline”in wpf

idv4meu8  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(217)

我正在使用<MaterialDesign:PackIcon Kind="BugOutline"/>和尝试相同的触发器。动机是更改焦点上的MaterialDesign:PackIcon Kind=“BugOutline”的边框颜色

<Button Margin="10 5" Command={Binding AddBugCommand} ToolTip="Add" Style="{StaticResource MaterialDesignIconButton}">
     <Material:PackIcon Kind="BugOutline"/>
  </Button>

我做了以下工作:

<MaterialDesign:PackIcon.Style>
   <Style TargetType="MaterialDesign:PackIcon">
     <Style.Triggers>
       <Triggers Property="IsFocused" Value="True">
          <Setter Property="BorderBrush" Value=""Yellow"/>
          <Setter Propety="BorderThickness" Value="2"/>
       </Triggers>
     </Style.Trigers>
   </Style>
</MaterialDesign:PackIcon.Style>

我还尝试通过扩展已有的按钮样式来为按钮本身创建触发器。但没有成功。

<Button.Style>
   <Style TargetType="{x:Type Button}" BasedOn={StaticResource MaterialDesignIconButton}>
     <Style.Triggers>
       <Trigger Property="IsFocused" Value="True">
           <Setter Property="BorderBrush" Value="Yellow"/>
           <Setter Property="BorderThickness" Value="2"/>
        <Trigger>
     </Style.Triggers>
    </Style>
</Button.Style>
fcipmucu

fcipmucu1#

下面是一个示例解决方案。

  • I将内容元素(在本例中为PackIcon)与按钮的前景绑定
  • 我创建了一个带有触发器的样式,它可以根据状态(IsMouseOver、IsKeyboardFocusWithin)更改按钮的前景。
<Button VerticalAlignment="Top"
     HorizontalAlignment="Left"
     Width="50"
     Height="50">
 <Button.Content>
     <Path
         Data="F1 M 9.97498,1.22334L 4.6983,9.09834L 4.52164,9.09834L 0,5.19331L 1.27664,3.52165L 4.255,6.08833L 8.33331,1.52588e-005L 9.97498,1.22334 Z "
         Fill="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}} ,Path=Foreground}"
         Margin="1"
         Opacity="1"
         Stretch="Fill" />
 </Button.Content>
 <Button.Style>
     <Style TargetType="{x:Type Button}">
         <Setter Property="Foreground"
                 Value="Red" />
         <Style.Triggers>
             <Trigger Property="IsMouseOver"
                      Value="True">
                 <Setter Property="Foreground"
                         Value="Yellow" />
             </Trigger>
             <Trigger Property="IsKeyboardFocusWithin"
                      Value="True">
                 <Setter Property="Foreground"
                         Value="Green" />
             </Trigger>
             <MultiTrigger>
                 <MultiTrigger.Conditions>
                     <Condition Property="IsMouseOver" Value="True"/>
                     <Condition Property="IsKeyboardFocusWithin" Value="True"/>
                 </MultiTrigger.Conditions>
                 <MultiTrigger.Setters>
                     <Setter Property="Foreground"
                             Value="Yellow" />
                 </MultiTrigger.Setters>
             </MultiTrigger>
         </Style.Triggers>
     </Style>
 </Button.Style>

如果你想适应我的例子,请不要忘记将BaseOn属性添加到样式中,将Path更改为PackIcon,并在触发时调整必要的画笔。

相关问题