如何在WPF中始终将弹出框放置在切换按钮下
本文关键字:按钮 WPF | 更新日期: 2023-09-27 18:16:40
我想在切换按钮下放置一个弹出框,当我点击它。在这个弹出框中,我想添加按钮和其他控件。但是我怎么能确保弹出窗口总是在我的切换按钮下,当我调整我的主窗口的大小。
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>
点击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;
}
使用Popup
的Placement
和PlacementTarget
属性:)
<Popup Placement="Bottom" PlacementTarget="{Binding ElementName=myToggleButton}" IsOpen="{Binding IsChecked, ElementName=Userbtn}" StaysOpen="False" />
这里要注意的是,如果当Popup
打开时窗口被移动,它不会移动。您可以做的是将IsOpen
属性绑定到ToggleButton
的IsChecked
,并设置StaysOpen
=False。这样做,弹出窗口将自动关闭,按钮切换回来:),如果你要这样做,你也不需要c#代码:)
或者这里是另一个SO帖子的链接,如果它应该保持打开,无论情况如何:移动WPF弹出窗口
希望有帮助