wpf 鼠标悬停时出现的弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13327278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:08:36  来源:igfitidea点击:

Popup appearing on mouse over

c#wpfpopuptogglebutton

提问by Geoffrey

I want a popup to appear every time I hover a togglebutton. It then needs to stay open until I click somewhere else in the application. The code below works fine on start up but as soon as I check or uncheck the togglebutton the popup refuses to appear. Any ideas on what I'm doing wrong?

我希望每次悬停切换按钮时都会出现一个弹出窗口。然后它需要保持打开状态,直到我单击应用程序中的其他位置。下面的代码在启动时工作正常,但是一旦我选中或取消选中切换按钮,弹出窗口就不会出现。关于我做错了什么的任何想法?

The WPF code

WPF 代码

        <ToggleButton Name="btnLogFile" Style="{StaticResource StandardToggle}"
                      Grid.Row="1" Grid.Column="3" Margin="0,3,3,0" 
                      MouseEnter="btnLogFile_MouseEnter">
            <Path Margin="7" SnapsToDevicePixels="True" Stretch="Uniform"
                Stroke="{StaticResource TextLight}" StrokeThickness="2">
                <Path.Data>
                    <GeometryGroup FillRule="Nonzero">
                        <PathGeometry Figures="M 0 0 L 20 0 L 20 10 L 30 10 L 30 40 L 0 40 Z" />
                        <PathGeometry Figures="M 20 0 L 22 0 L 30 8 L 30 10" />
                    </GeometryGroup>
                </Path.Data>
            </Path>
        </ToggleButton>
        <Popup Name="popLogFile"
               PlacementTarget="{Binding ElementName=btnLogFile}" Placement="Custom"
               HorizontalOffset="0" VerticalOffset="0"
               MouseLeftButtonDown="popLogFile_MouseLeftButtonDown">
            <Border Background="{StaticResource BackgroundDark}" BorderBrush="{StaticResource TextBoxBorder}" BorderThickness="1"
                    Width="300" Height="Auto">
                <Grid Margin="3">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="3" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="3" />
                        <ColumnDefinition Width="22" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="22" />
                    </Grid.RowDefinitions>

                    <TextBlock Margin="0,1" Grid.Row="0" Grid.Column="0" Foreground="{StaticResource TextLight}" HorizontalAlignment="Right">Directory</TextBlock>
                    <TextBox Name="logfilePath" Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="3"
                             Style="{StaticResource StandardTextBox}"
                             Foreground="{StaticResource TextLight}">
                        C:\logfile.txt
                    </TextBox><!-- Button made invisible for the time being -->
                    <Button Name="btnBrowseLogfile" Style="{StaticResource StandardButton}" Grid.Row="0" Grid.Column="4" Visibility="Collapsed">...</Button>

                </Grid>
            </Border>
        </Popup>

And the togglebutton's mouse event:

以及切换按钮的鼠标事件:

    private void btnLogFile_MouseEnter(object sender, MouseEventArgs e)
    {
        this.popLogFile.IsOpen = true;
        this.popLogFile.StaysOpen = false;
    }

回答by Federico Berasategui

Do not use code to manipulate UIElements. Bind the IsOpenProperty of the Popup to the IsMouseOverproperty of the togglebutton. Or else if you need multiple or more complex conditions, set a Styleto the Popup and in the style you can include Triggersor DataTriggers. I suggest you take a look at this WPF Tutorial

不要使用代码来操作 UIElements。IsOpen将 Popup 的IsMouseOver属性绑定到切换按钮的属性。或者,如果您需要多个或更复杂的条件,请将 a 设置Style为 Popup 并在您可以包含的样式中设置TriggersDataTriggers。我建议你看看这个WPF 教程

Edit:

编辑:

Should be something like:

应该是这样的:

<DataTrigger Binding="{Binding IsChecked, ElementName=YourToggleButton}" Value="True">
   <Setter TargetName="ThePopup" Property="IsOpen" Value="True"/>
</DataTrigger>

回答by Mate

1) Add event triggers to ToggleButton:

1) 向 ToggleButton 添加事件触发器:

       <ToggleButton Name="btnLogFile" 
                  Style="{StaticResource StandardToggle}"
                  Grid.Row="1" Grid.Column="3" Margin="0,3,3,0" >
        <ToggleButton.Triggers>
            <EventTrigger RoutedEvent="MouseEnter">
                <BeginStoryboard>
                    <Storyboard TargetName="popLogFile" TargetProperty="IsOpen">
                        <BooleanAnimationUsingKeyFrames  FillBehavior="HoldEnd">
                            <DiscreteBooleanKeyFrame
                    KeyTime="00:00:00"
                    Value="True" />
                        </BooleanAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>

            <EventTrigger RoutedEvent="ToggleButton.Checked">
                <BeginStoryboard>
                    <Storyboard TargetName="popLogFile" TargetProperty="IsOpen">
                        <BooleanAnimationUsingKeyFrames  FillBehavior="HoldEnd">
                            <DiscreteBooleanKeyFrame
                    KeyTime="00:00:00"
                    Value="False" />
                        </BooleanAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>

            <EventTrigger RoutedEvent="ToggleButton.Unchecked">
                <BeginStoryboard>
                    <Storyboard TargetName="popLogFile" TargetProperty="IsOpen">
                        <BooleanAnimationUsingKeyFrames  FillBehavior="HoldEnd">
                            <DiscreteBooleanKeyFrame
                    KeyTime="00:00:00"
                    Value="False" />
                        </BooleanAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>

        </ToggleButton.Triggers>

        <Path Margin="7" SnapsToDevicePixels="True" Stretch="Uniform"
        ...

2) Set in XAML StaysOpen="False" :

2) 在 XAML 中设置 StaysOpen="False" :

<Popup Name="popLogFile"
           PlacementTarget="{Binding ElementName=btnLogFile}" Placement="Custom"
           HorizontalOffset="0" VerticalOffset="0"
           StaysOpen="False"
           MouseLeftButtonDown="popLogFile_MouseLeftButtonDown">

3) Remove method btnLogFile_MouseEnter

3) 移除方法 btnLogFile_MouseEnter

回答by Frank

You can bind Popup.IsOpen and ToggleButton.IsChecked to the same bool property. See demo code here. http://bit.ly/L5jxFl

您可以将 Popup.IsOpen 和 ToggleButton.IsChecked 绑定到同一个 bool 属性。在此处查看演示代码。http://bit.ly/L5jxFl