WPF中的Expander按钮

时间:2014-08-30 11:29:08

标签: c# wpf xaml

我是WPF,C#和XAML的新手(来自obj-c)我只想弄清楚如何将它放到隐藏扩展器控件的按钮的位置,并在鼠标上显示?感谢

1 个答案:

答案 0 :(得分:1)

为此,您必须在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>