我是WPF的新手。我需要将此xaml代码与ControlTemplate分开。 这是我的原始代码。
<ControlTemplate x:Key="AddImgBtnTemplate" TargetType="Button" >
<Grid>
<Border Name="border" Height="25" Width="25"/>
<Image Name="Normal" Source="/UILibrary;component/Themes/Default/Images/Add.png" Height="18" Width="18"/>
<Image Name="Pressed" Source="/UILibrary;component/Themes/Default/Images/Add.png" Opacity="0.7" Visibility="Hidden" Height="18" Width="18"/>
<Image Name="Disabled" Source="/UILibrary;component/Themes/Default/Images/Add.png" Opacity="0.3" Visibility="Hidden" Height="18" Width="18"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Disabled" Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="CornerRadius" Value="3" />
<Setter TargetName="border" Property="BorderThickness" Value="1" />
<Setter TargetName="border" Property="BorderBrush" Value="#FF948A54" />
</Trigger>
</ControlTemplate.Triggers>
需要分离代码这段代码。这段代码是重复代码,只需要定义一些代码。
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Disabled" Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="CornerRadius" Value="3" />
<Setter TargetName="border" Property="BorderThickness" Value="1" />
<Setter TargetName="border" Property="BorderBrush" Value="#FF948A54" />
</Trigger>
</ControlTemplate.Triggers>
答案 0 :(得分:0)
您可以将带有触发器的代码放入Style
,就像这样。
<Style x:Key="myButton" TargetType="Button">
<Style.Triggers>
</Style.Triggers>
</Style>
您可以在Template
或其他按钮中使用此样式。
<Button Style="{StaticResource myButton}"/>