WPF中的扩展器按钮
本文关键字:按钮 扩展器 WPF | 更新日期: 2023-09-27 18:20:01
我是WPF、C#和XAML的新手(来自obj-C),我只是想知道如何将它放在扩展器控件的按钮隐藏的位置,并显示在鼠标上?感谢
为此,您必须在ControlTemplate的帮助下自定义扩展器控件。您可以在切换按钮的控件模板中有一个图像,默认情况下其来源不会是任何东西。鼠标悬停时,可以给出图像的路径,并且图像将可见。
<ControlTemplate x:Key="ExpanderToggleButtonTemplate" TargetType="{x:Type ToggleButton}">
<Border x:Name="ExpanderToggleButtonBorder" Height="30">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Image Name="img" HorizontalAlignment="Left"></Image>
<ContentPresenter x:Name="HeaderContent" Grid.Column="0" Margin="50,0,0,0"
ContentSource="Content" >
</ContentPresenter>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="true">
<Setter Property="Source"
Value="path of whichever image is required"
TargetName="img"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate x:Key="ExpanderTemplate" TargetType="{x:Type Expander}">
<DockPanel>
<ToggleButton DockPanel.Dock="Top"
Template="{StaticResource ExpanderToggleButtonTemplate}" Content="{TemplateBinding Header}"
IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" OverridesDefaultStyle="True">
</ToggleButton>
<ContentPresenter x:Name="ExpanderContent" Grid.Row="1" Visibility="Collapsed" DockPanel.Dock="Bottom"/>
</DockPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="True">
<Setter TargetName="ExpanderContent" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>