How to always place PopUp under a ToggleButton in WPF

情到浓时终转凉″ 提交于 2019-12-02 03:19:46

问题


I want to place a popup under a togglebutton when I click on it. In this popup I want to add button and other controls. But how can I make sure that the popup window always will be under my togglebutton also when I resize my mian window.

My XAML code:

     <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>

C# code for click on togglebutton:

    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:


Use Placement and PlacementTarget properties of the Popup :)

<Popup Placement="Bottom" PlacementTarget="{Binding ElementName=myToggleButton}" IsOpen="{Binding IsChecked, ElementName=Userbtn}" StaysOpen="False" />

The thing to note here is that, if the window is moved when Popup is open, it wont move along.. What you could do is Bind the IsOpen property to ToggleButton's IsChecked and set StaysOpen=False. Doing that, the Popup will close automatically, toggling the button back :) , also you wont be needing the C# code if you are to do this :)

OR here is a link to another SO post if it should stay open whatever the case be : Move a WPF Popup

Hope it helps :)



来源:https://stackoverflow.com/questions/32992577/how-to-always-place-popup-under-a-togglebutton-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!