我可以更改弹出框的对齐方式,使其显示在左侧而不是按钮的右侧吗?

时间:2013-01-10 21:56:18

标签: c# wpf xaml popup togglebutton

关于如何移动这个职位的任何想法?这是菜单:

enter image description here

这是XAML:

<Canvas MinHeight="25" Panel.ZIndex="99">
<ToggleButton x:Name="btnPluginMenu" Content="CT" Width="48" Height="48" Canvas.Left="452" Canvas.Top="263"/>
<Popup Placement="Top" PlacementTarget="{Binding ElementName=btnPluginMenu}" IsOpen="{Binding ElementName=btnPluginMenu, Path=IsChecked}">
<Grid Background="Gray" Width="175" Height="130">
                    <Grid.BitmapEffect>
                        <DropShadowBitmapEffect />
                    </Grid.BitmapEffect>
 </Grid>
            </Popup>
</Canvas>

2 个答案:

答案 0 :(得分:4)

MSDN开始,您可以使用Placement=Relative模式:

  

Popup控件相对于PlacementTarget左上角的位置,以及由Horizo​​ntalOffset和VerticalOffset属性值定义的偏移量。如果屏幕边缘遮挡了弹出窗口,则控件会重新定位以与屏幕边缘对齐。

换句话说,你可以这样做:

<ToggleButton x:Name="btnPluginMenu" Content="CT" Width="48" Height="48" Canvas.Left="452" Canvas.Top="263"/>
<Popup Placement="Relative"  HorizontalOffset="-120" VerticalOffset="-130" PlacementTarget="{Binding ElementName=btnPluginMenu}" IsOpen="{Binding ElementName=btnPluginMenu, Path=IsChecked}">

请注意使用负值HorizontalOffsetVerticalOffset

答案 1 :(得分:-1)

来自按钮点击的活动

private void button_Click(object sender, RoutedEventArgs e)
{
  Button button = sender as Button;
  popup.PlacementTarget = button;
  popup.HorizontalOffset = -120;
  popup.VerticalOffset = -130;
  popup.IsOpen = true;
}