使DropDownButton在IsMouseOver事件上自动打开

时间:2016-06-24 16:48:28

标签: c# wpf xaml mvvm mahapps.metro

我正在使用Mahapps和MVVM Light。我试图让DropDownButton在IsMouseOver上扩展并在鼠标离开时收缩。 我尝试过不同的方法来做到这一点。但没有结果。 第一个。

        <Style x:Key="AutoOpenDropDownButtonStyle" TargetType="{x:Type controls:DropDownButton}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="IsExpanded" Value="True" />
            </Trigger>
        </Style.Triggers>
    </Style>

            <controls:DropDownButton x:Name="ddbVolume" Width="{Binding Path=ActualHeight, RelativeSource={RelativeSource Self}}"
                                     ItemsSource="{Binding AudioControls}"
                                     Icon="{DynamicResource appbar_settings}" BorderThickness="0"
                                     ArrowVisibility="Collapsed" Style="{StaticResource AutoOpenDropDownButtonStyle}">
            </controls:DropDownButton>

但它根本不起作用。

此外,我尝试了另一种解决方案。

                <controls:DropDownButton x:Name="ddbVolume" Width="{Binding Path=ActualHeight, RelativeSource={RelativeSource Self}}"
                                     ItemsSource="{Binding AudioControls}"
                                     Icon="{DynamicResource appbar_settings}" BorderThickness="0"
                                     ArrowVisibility="Collapsed">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseEnter">
                        <command:EventToCommand Command="{Binding MouseEnterCommand}" CommandParameter="{Binding ElementName=ddbVolume}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>

                <command:EventToCommand Command="{Binding MouseEnterCommand}" PassEventArgsToCommand="True"/>
            </controls:DropDownButton>

    MouseEnterCommand = new RelayCommand<DropDownButton>(MouseVolumeBarEnter, x => true);
    MouseLeaveCommand = new RelayCommand<DropDownButton>(MouseVolumeBarLeave, x => true);

    private void MouseVolumeBarLeave(DropDownButton sender)
    {
        if (sender.IsExpanded)
        {
            sender.IsExpanded = false;
        }
    }

    private void MouseVolumeBarEnter(DropDownButton sender)
    {
        if (!sender.IsExpanded)
        {
            sender.IsExpanded = true;
        }
    }

第二种解决方案也不起作用。我无法将此事件设置为内​​部DropDownButton ContextMenu,其中显示了内容项列表。 我试图创建自己的Mahapps DropDownButton分支(将其命名为AutoOpenDropDownButton)我取https://github.com/MahApps/MahApps.Metro/blob/develop/MahApps.Metro/Controls/DropDownButton.cs并重命名它们并在文件https://github.com/MahApps/MahApps.Metro/blob/develop/MahApps.Metro/Themes/DropDownButton.xaml中我在第283行添加了IsMouseOver触发器,在第290行添加了setter)。但控件没有显示。

1 个答案:

答案 0 :(得分:0)

是否需要在下拉按钮上应用样式,您可以轻松地使其适用于ComboBox的样式。使用简单的触发器

<Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        Setter Property="IsDropDownOpen" Value="True"/>
    </Trigger>
</Style.Triggers>