如何始终将PopUp置于WPF中的ToggleButton下

时间:2015-10-07 12:44:47

标签: c# wpf popup togglebutton

当我点击它时,我想在一个togglebutton下面放一个弹出窗口。在这个弹出窗口中,我想添加按钮和其他控件。但是,当我调整mian窗口的大小时,如何确保弹出窗口始终位于我的togglebutton下面。

我的XAML代码:

     <ToggleButton
    x:Name="userBtn"
    Margin="610,25,378,0"
    VerticalAlignment="Top"            
    Height="29"
    Grid.Column="2" 
    >
        <ToggleButton.Resources>
            <BitmapImage x:Key="imgNormal" UriSource="/Resources/home.jpg"/>
            <BitmapImage x:Key="imgHover" UriSource="/Resources/home_checked.jpg"/>
            <BitmapImage x:Key="imgChecked" UriSource="/Resources/home_checked.jpg"/>
        </ToggleButton.Resources>
        <ToggleButton.Style>
            <Style TargetType="ToggleButton">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ToggleButton" >
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Width="150" Height="32">
                                <Image x:Name="PART_Image" Height="22" Width="22" RenderOptions.BitmapScalingMode="HighQuality" HorizontalAlignment="Center" Margin="0,0,0,0" Source="{StaticResource imgNormal}"/>
                                <TextBlock x:Name="PART_TEXT" FontFamily="Arial" Foreground="#FFAFADAD" FontSize="13" HorizontalAlignment="Center" Text="{x:Static properties:Resources.BtnDashboard}"  Margin="10,9,0,0"></TextBlock>
                            </StackPanel>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked" Value="true">
                                    <Setter TargetName="PART_Image" Property="Source" Value="{StaticResource imgChecked}"/>
                                    <Setter TargetName="PART_TEXT" Property="Foreground" Value="#79B539" />
                                </Trigger>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter TargetName="PART_Image" Property="Source" Value="{StaticResource imgHover}"/>
                                    <Setter TargetName="PART_TEXT" Property="Foreground" Value="#79B539" />
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter TargetName="PART_Image" Property="Opacity" Value="0.6"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ToggleButton.Style>
    </ToggleButton>

    <Popup Name="UserMenuPopUp" PopupAnimation="Fade" Width="280" Height="auto" Margin="20" AllowsTransparency="True" HorizontalAlignment="Left" Placement="bottom" PlacementTarget="{Binding ElementName=userBtn}" IsOpen="{Binding IsChecked, ElementName=Userbtn}" StaysOpen="false" >
        <Border Margin="2,0,48,0" BorderThickness="1" Background="#EEEEEE" Height="105">
            <Grid>

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

单击togglebutton的C#代码:

    private void UserBtn_Click(object sender, RoutedEventArgs e)
    {
        // Get location, adn sender
        var element = (System.Windows.Controls.Primitives.ToggleButton)sender;
        var location = element.PointToScreen(new Point(0, 0));

        // Set popup location
        UserMenuPopUp.HorizontalOffset = (location.X);
        UserMenuPopUp.VerticalOffset = (location.Y);
        UserMenuPopUp.IsOpen = true;
    }

1 个答案:

答案 0 :(得分:4)

使用Placement的{​​{1}}和PlacementTarget属性:)

Popup

这里要注意的是,如果在<Popup Placement="Bottom" PlacementTarget="{Binding ElementName=myToggleButton}" IsOpen="{Binding IsChecked, ElementName=Userbtn}" StaysOpen="False" /> 打开时移动窗口,它就不会移动..你可以做的是将Popup属性绑定到IsOpenToggleButton并设置IsChecked = False。这样做,弹出窗口将自动关闭,切换回按钮:),如果你要这样做,你也不需要C#代码:)

或者,如果它应该保持打开,这里是另一个SO帖子的链接: Move a WPF Popup

希望有所帮助:)